https://www.focustread.com/training
WHAT IS XSLT
It is a programming language for processing XML data i.e transforming XML documents based on the requirment.
https://www.focustread.com/training
In the dynamic world we would need convert the required input file to the required file . This is easy to learn as it is based on xml only. Gives us very versatile list of option for the conversion. Building block for xml based technologies.
https://www.focustread.com/training
Input is a XML
XSLT Processor
https://www.focustread.com/training
REQUIRMENT(SAXON PROCESSOR)
https://www.focustread.com/training
In order to convert the xml into the required the xml or html , a XSLT processor is needed . This processor takes into input the .xslt file and based on the declaration done in the .xslt file the required output is generated. SYNTAX
A SIMPLE EXAMPLE
people.xml <people> <person> <Name>Andy</Name> <Description> he is Java teacher</Description> </person> <person> <Name>Candy</Name> <Description>She is oracle teacher</Description> </person> </people>
https://www.focustread.com/training
FIRST.XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match ="/"> <html> <head> <title>Information about <xsl:value-of select="count(/people/person)" /> people </title> </head> <body><h3> Information about <xsl:value-of select="count(/people/person)" /> </h3> <br/> <xsl:apply-templates select="/people/person" />
</body> </html> </xsl:template> <xsl:template match="person"> <h3><xsl:value-of select="Name" /></h3> <p> <xsl:value-of select="Description" /></p> <br /> </xsl:template> </xsl:stylesheet>
https://www.focustread.com/training
<XSL:STYLESHEET> ELEMENT
This is the first element of the xslt which defines the version and Namespace of the xslt.
example
https://www.focustread.com/training
<XSL:TEMPLATE> ELEMENT
The xslt processor looks for the template tag with the match open as /
when xslt processor find a node of this type it then then start creating the result xml/html.
https://www.focustread.com/training
<XSL:VALUE-OF> ELEMENT
This element provides the values from the source xml , based on the condition provided in it .
It has a mandatory attribute select , which is used to provide the condition to based on which respective values from the source xml are selected.
https://www.focustread.com/training
<XSL-COPY> ELEMENT
This element copies the a node to the result tree , but it doesnt copy any descendant nodes .
If the context node is a element then it doesnt copy any of it attribute. This is useful you want to change the structure of the element i.e add or remove its attributes .
https://www.focustread.com/training
<XSL:COPY> EXAMPLE
<persons> <person> <FirstName>Andy</FirstName> <LastName>Jackson</LastName> </person> <person> <FirstName>Candy</FirstName> <LastName>Soni</LastName> </person> </persons>
https://www.focustread.com/training
CONVERTCOPY.XSLT
<xsl:template match="/"> <persons> <xsl:apply-templates select="/persons/person" /> </persons> </xsl:template> <xsl:template match="person"> <xsl:copy> <xsl:attribute name="FirstName"><xsl:value-of select="FirstName"/> </xsl:attribute> <xsl:attribute name="LastName"><xsl:value-of select="LastName"/> </xsl:attribute> </xsl:copy> </xsl:template> </xsl:stylesheet> https://www.focustread.com/training
CONVERT IT BACK
<xsl:template match="/"> <persons> <xsl:apply-templates select="/persons/person" /> </persons> </xsl:template> <xsl:template match="person"> <xsl:copy> <xsl:element name="FirstName"><xsl:value-of select="@FirstName"/> </xsl:element> <xsl:element name="LastName"><xsl:value-of select="@LastName"/> </xsl:element> </xsl:copy> </xsl:template> </xsl:stylesheet> https://www.focustread.com/training
RESULT.XML
<?xml version="1.0" encoding="UTF-8"?> <persons> <person FirstName="Andy" LastName="Jackson"/> <person FirstName="Candy" LastName="Soni"/> </persons>
https://www.focustread.com/training
<XSL:COPY-OF> ELEMENT
It does the deep copy . It copies ( a node,its attribute and its descendants) all of them to the resultant tree.
https://www.focustread.com/training
(PURCHASEORDER TO INVOICE)
Invoice.xml <?xml version="1.0" encoding="UTF-8"?> <Invoice> <To>USA</To> <From>India</From> <Address> <Street>234 Any Street</Street> <City>My City</City> <ZipCode>1122334</ZipCode> </Address> </Invoice>
https://www.focustread.com/training
(PURCHASEORDER TO INVOICE)
PurchaseOrder.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <Invoice> <xsl:apply-templates select="/PurchaseOrder/From" /> <xsl:apply-templates select="/PurchaseOrder/To" /> <xsl:apply-templates select="/PurchaseOrder/Address" /> </Invoice> </xsl:template> <xsl:template match="From"> <xsl:element name="To"><xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="To"> <xsl:element name="From"><xsl:value-of select="."/> </xsl:element> </xsl:template> <xsl:template match="Address"> <xsl:copy-of select="." /> </xsl:template> </xsl:stylesheet>
https://www.focustread.com/training
<XSL:OUTPUT> ELEMENT
This element is being used to call out which type of output we need as the result.
CONDITIONAL PROCESSING In Xslt you have following option for conditional processing
1.
<xsl:if>.
<XSL:IF> ELEMENT
If the result is true then content of xsl:if are instantiated and added to the result tree.
https://www.focustread.com/training
https://www.focustread.com/training
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> <title> This is for the Test </title> <body> </head> <h3> The Record of the age is very high</h3> </body> </xsl:template> </html> <xsl:template match="Character"> <xsl:if test ="@age > 110"> </xsl:if> </xsl:template> </xsl:stylesheet> The age of <xsl:value-of select="." /> is <xsl:value-of select="@age" /> <br/> <xsl:apply-templates select="/Characters/Character" />
xslifExample.xslt
https://www.focustread.com/training
<XSL:CHOOSE> ELEMENT
This works almost like if then-else statment . This is useful if you want your code to take some decision based on the tested condition is true or false.
Example <xsl:choose> <xsl:when test=condition> code to display when condition is true. </xsl:when> <xsl:otherwise> Code will come here when the condition is false </xsl:otherwise> </xsl:choose> https://www.focustread.com/training
<XSL:CHOOSE> EXAMPLE
xslchooseExample.xml <Characters> <Character age="10">First Person</Character> <Character age="110">First Person</Character> <Character age="120">First Person</Character> <Character age="30">First Person</Character> <Character age="40">First Person</Character> <Character age="150">First Person</Character> </Characters>
https://www.focustread.com/training
<XSL:CHOOSE> EXAMPLE
xslchooseExample.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> <title> This is for the Test </title> </head> <body> <h3> The Record of the age is very high</h3> <xsl:apply-templates select="/Characters/Character" /> </body> </html> </xsl:template> <xsl:template match="Character"> <xsl:choose> <xsl:when test ="@age > 110"> The age of <xsl:value-of select="." /> is <xsl:value-of select="@age" /> is very high <br/> </xsl:when> <xsl:otherwise> The age of <xsl:value-of select="." /> is <xsl:value-of select="@age" /> is very okay <br/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
https://www.focustread.com/training
<XSL:FOR-EACH>
It is being used to process all the nodes in the in the node-set according to the XSLT instructions nested inside the xsl:for-each element.
https://www.focustread.com/training
<XSL:FOR-EACH> EXAMPLE
xslforeachExample.xml <Objects> <Object name="Car"> <Characteristic>Hard</Characteristic> <Characteristic>Shiny</Characteristic> <Characteristic>4 wheels</Characteristic> <Characteristic>2 people capacity</Characteristic> </Object> <Object name="Orange"> <Characteristic>Soft</Characteristic> <Characteristic>Orange</Characteristic> <Characteristic>Sweet</Characteristic> </Object> https://www.focustread.com/training </Objects>
<XSL:FOR-EACH> EXAMPLE
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> <title> This is for-Each Test </title> <body> </head> <h3> This is for-Each Test</h3> <xsl:apply-templates select="/Objects/Object" /> </body> </xsl:template> </html> <xsl:template match="Object"> <xsl:for-each select ="Characteristic"> <li><xsl:value-of select="." /></li> <br/> </xsl:for-each> </xsl:template> </xsl:stylesheet> Shows the result of <xsl:value-of select="@name" />
xslforeachExample.xslt
https://www.focustread.com/training
<XSL:SORT> ELEMENT
https://www.focustread.com/training
<XSL:SORT>EXAMPLE
xslsort.xml <Objects> <Object name="Car"> <Characteristic>Hard</Characteristic> <Characteristic>Shiny</Characteristic> <Characteristic>4 wheels</Characteristic> <Characteristic>2 people capacity</Characteristic> </Object> <Object name="Orange"> <Characteristic>Soft</Characteristic> <Characteristic>Orange</Characteristic> <Characteristic>Sweet</Characteristic> </Object> https://www.focustread.com/training </Objects>
<XSL:SORT>EXAMPLE
xslsort.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> <title> This is Sort Test </title> </head> <body> <h3> This is Sort Test</h3> <xsl:apply-templates select="/Objects/Object" > <xsl:sort select="@name" /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="Object"> Shows the result of <xsl:value-of select="@name" /> <xsl:for-each select ="Characteristic"> <xsl:sort select="." /> <li><xsl:value-of select="." /></li> <br/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
https://www.focustread.com/training
<xsl:variable /> <xsl:parameter /> They act like constants and there value cannot change once they are set.
https://www.focustread.com/training