transforming xml with xslt

25
Transforming XML with XSLT Basics and understanding Malintha Adikari Software Engineer

Upload: malintha-adikari

Post on 13-Apr-2017

559 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Transforming  xml with XSLT

Transforming XML with XSLT

Basics and understanding

Malintha Adikari

Software Engineer

Page 2: Transforming  xml with XSLT

What is XSLT

XSL stands for

EXtensible Stylesheet Language, and is a style sheet language for XML documents.

XSLT stands for

XSL Transformations

Page 3: Transforming  xml with XSLT

XSL vs CSS

Page 4: Transforming  xml with XSLT

Goals of XSLT

● XSLT is a transformation language for XML

● XSLT is a W3C XML language (the usual XML well-formedness criteria apply)

● XSLT can translate XML into almost anything , e.g.:○ well-formed HTML (closed tags)○ any XML, e.g. yours or other XML languages like SVG, X3D○ non XML, e.g. PDF,ZIP...etc

Page 5: Transforming  xml with XSLT

XML Transformation

XML, XHTML, PDF, Plain text….

Page 6: Transforming  xml with XSLT

Why transforming

o Filter

Page 7: Transforming  xml with XSLT

Why transforming

o Presentations

Page 8: Transforming  xml with XSLT

Why transforming

o Formatting

Page 9: Transforming  xml with XSLT

XPath Recap..

XPath uses path expressions to navigate in XML documentsLet’s try following example

Page 10: Transforming  xml with XSLT

XPath Recap..Example 1:

Example 2:

Page 11: Transforming  xml with XSLT

Start with XSLT

<?xml version="1.0"?>

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

<xsl:template match="/"> <html> <body> …….. </xsl:stylesheet>

● XSLT program is XML document

● XML declaration on top of the file

● A stylesheet root tag with the following

version and namespace attributes (The root

element that declares the document to be an

XSL style sheet is <xsl:stylesheet> or <xsl:

transform>)

● Usually the "xsl" prefix is used for XSLT tags,

but you also can find "xs" or none

● XSLT must be well-formed and valid

● XSLT files usually have the *.xsl extension

and should have the text/xsl or

application/xml mime type

● XSLT uses XPath to find information in an

XML document.

Page 12: Transforming  xml with XSLT

Associate XML file and XSLT file

You can directly associate a XSLT stylesheet with an XML file by using a so-called processing instruction

o Most programming languages and all well-know server-side scripting languages include an XSLT library, so you can tell program to apply an XSLT stylesheet to an XML file

o use a xml-stylesheet instruction if you plan to display XML contents directly in a browser

Page 13: Transforming  xml with XSLT

Example

XML file

<?xml version="1.0" encoding="UTF-8"?>

<catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd></catalog>

Expected output (HTML)

Page 14: Transforming  xml with XSLT

Example

XML file

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="myStyle.xsl"?><catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd></catalog>

XSL file (myStyle.xsl)

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html"/><xsl:template match="/"> <html> <body> <h2>My CD Collection</h2>

<table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>

</table> </body> </html></xsl:template></xsl:stylesheet>

Page 15: Transforming  xml with XSLT

How XSLT works

1. The XSLT engine first looks at the XML file and tries to find a XLT rule that will match the root element

2. The XSLT processor will then "move" inside the rule element and do further processing

o HTML elements (or any other tags) will be copied to the output documento If an XSLT instruction is found, it will be executed

● XSLT works down "depth-first" the XML tree, i.e.

● it first deals with the rule for the root element,● then with the first instruction within this rule.● If the first instruction says "find other rules" it will then apply the first rule found for the first

child element and so forth...● The rule of the root element is also the last one be finished (since it must deal step-by-step

with everything that is found inside) !!!

Page 16: Transforming  xml with XSLT

Example

XML file

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="myStyle.xsl"?><catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd></catalog>

XSL file (myStyle.xsl)

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html"/><xsl:template match="/"> <html> <body> <h2>My CD Collection</h2>

<table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>

</table> </body> </html></xsl:template></xsl:stylesheet>

Page 17: Transforming  xml with XSLT

XSLT elements

<xsl:template> Element

The <xsl:template> element is used to build templates.The match attribute is used to associate a template with an XML element. The value of the match attribute is an XPath expression

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="tool"> ………. </xsl:template><xsl:template match="paint"> ………. </xsl:template>

<?xml version="1.0" encoding="UTF-8"?><tool> <field id="prodName"> <value>HAMMER HG2606</value> </field></tool> <paint> <field id="prodName"> <value>mypaint001</value> </field></paint>

Page 18: Transforming  xml with XSLT

XSLT elements

<xsl:value-of> Element

The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="/"> <h1><xsl:value-of select="tool/field[@id=’prodName’]/value"/> </h1> </xsl:template>

<?xml version="1.0" encoding="UTF-8"?><tool> <field id="prodName"> <value>HAMMER HG2606</value> </field></tool> <paint> <field id="prodName"> <value>mypaint001</value> </field></paint>

Page 19: Transforming  xml with XSLT

XSLT elements

<xsl:for-each> ElementThe XSL <xsl:for-each> element can be used to select every XML element of a specified node-set

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/"> <html> <body> <h2>My Collection</h2> <table border="1"> <th>Tool</th> <th>Value</th> </tr>

<xsl:for-each select="tool"> <tr> <td><xsl:value-of select="field[@id]"/></td> <td><xsl:value-of select="field/value"/></td> </tr>

</xsl:for-each> </table> </body> </html></xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?><tool> <field id="prodName"> <value>HAMMER HG2606</value> </field></tool> <tool> <field id="prodName"> <value>myTool</value> </field></tool>

Page 20: Transforming  xml with XSLT

XSLT elements

<xsl:apply-templates> ElementThe <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates select=”cd”/> </body> </html></xsl:template><xsl:template match="cd"> <p>

<xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/>

</p></xsl:template><xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /></xsl:template><xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /></xsl:template></xsl:stylesheet>

<<?xml version="1.0" encoding="UTF-8"?><catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> </catalog>

Page 21: Transforming  xml with XSLT

XSLT elements

The <xsl:if> ElementTo put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

Syntax<xsl:if test="expression"> ...some output if the expression is true...</xsl:if>

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> ………… <xsl:for-each select="catalog/cd"> <xsl:if test="price>10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if></xsl:for-each></xsl:template></xsl:stylesheet>

Page 22: Transforming  xml with XSLT

XSLT elements

XSLT <xsl:sort> Element

The <xsl:sort> element is used to sort the output.

For more XSLT elements and functions :http://www.w3schools.com/xsl/default.asp

XSLT <xsl:choose> Element

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

Syntax<xsl:choose><xsl:when test="expression">... some output ...

</xsl:when>

<xsl:otherwise>

... some output ....

</xsl:otherwise>

</xsl:choose>

Page 23: Transforming  xml with XSLT

Exercises

1. Download “automation.xml” file from following svn locationhttps://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/platform-integration/test-automation-framework-2/org.wso2.

carbon.automation.engine/4.3.0/src/main/resources/automation.xml

2. Go to user management section and look at it’s current structure

3. Every “tenant” element has “admin”child and “admin”child has “user” element. You have to get that user element and put it in to “users” element. New structure should look like this

<tenant domain="wso2.com" key="wso2"> <users>

<user key="admin"> <userName>admin</userName> <password>admin</password> </user> <user key="user1"> <userName>testuser11</userName> <password>testuser11</password> </user> </users></tenant>

Other elements remains same as original file. Write the output into new xml file.

Page 24: Transforming  xml with XSLT

Questions?