0% found this document useful (0 votes)
155 views35 pages

XSLT (EXtensible Style Sheet Language)

XSLT is a programming language used to transform XML documents into other formats like HTML or other XML. It allows converting an input XML file into the required output format using XSLT style sheets. An XSLT processor is needed to perform the transformation which takes the XML and XSLT files as input and produces the output based on the declarations in the XSLT file. Some key elements in XSLT include <xsl:template> to define templates, <xsl:value-of> to output values from the XML, and <xsl:copy-of> for deep copying of nodes between trees.

Uploaded by

varma98
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views35 pages

XSLT (EXtensible Style Sheet Language)

XSLT is a programming language used to transform XML documents into other formats like HTML or other XML. It allows converting an input XML file into the required output format using XSLT style sheets. An XSLT processor is needed to perform the transformation which takes the XML and XSLT files as input and produces the output based on the declarations in the XSLT file. Some key elements in XSLT include <xsl:template> to define templates, <xsl:value-of> to output values from the XML, and <xsl:copy-of> for deep copying of nodes between trees.

Uploaded by

varma98
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

XSLT(EXTENSIBLE STYLESHEET LANGUAGE)

https://www.focustread.com/training

WHAT IS XSLT

From a programmers view

It is a programming language for processing XML data i.e transforming XML documents based on the requirment.

https://www.focustread.com/training

WHY WOULD YOU NEED XSLT

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

HIGH LEVEL VIEW OF WHAT IS XSLT


DOING

Input is a XML

Xslt file needed for processing

XSLT Processor

Can produce a html/xhtml

Can produce another xml

https://www.focustread.com/training

REQUIRMENT(SAXON PROCESSOR)

Please download the Tool from http://saxon.sourceforge.net/

Please download the latest version for this.

https://www.focustread.com/training

WHAT IS XSLT PROCESSOR

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

Java jar saxon<version>.jar <inputfile.xml> <inputfile.xslt> < resultfile.xml/html>


https://www.focustread.com/training

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transfor m" version="1.0">

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.

Example <xsl:template match=/ > </xsl:template>

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:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<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:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<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.

Example <xsl:copy-of select=condition> </xsl:copy-of>


https://www.focustread.com/training

<XSL:COPY-OF> EXAMPLE (PURCHASEORDER TO INVOICE)


PurchaseOrder.xml <PurchaseOrder> <From>USA</From> <To>India</To> <Address> <Street>234 Any Street</Street> <City>My City</City> <ZipCode>1122334</ZipCode> </Address> </PurchaseOrder>

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.

Syntax for xml output <xsl :output method=xml>

Syntax for html output <xsl :output method=html>


https://www.focustread.com/training

CONDITIONAL PROCESSING In Xslt you have following option for conditional processing
1.

<xsl:if>.

2. <xsl:choose> 3.<xsl:for-each> 4. <xsl: sort>


https://www.focustread.com/training

<XSL:IF> ELEMENT

It tests xsl:if element for boolean result .

If the result is true then content of xsl:if are instantiated and added to the result tree.

If the result id fale the nothing is being added.

https://www.focustread.com/training

<XSL:IF > EXAMPLE


xslifExample.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:IF > EXAMPLE

<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 &gt; 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 &gt; 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.

Syntax <xsl:for-each select =condition> Code which is to be processed. </xsl:for-each>

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

This is used to specify the sort order of the nodesets .

This can work with a. xsl:template-apply element. b. xsl:for-each 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> AND <XSL:PARAMETER>

They are mentioned by below mentioned syntax

<xsl:variable /> <xsl:parameter /> They act like constants and there value cannot change once they are set.

https://www.focustread.com/training

You might also like