11

XSL – Extensible Style Sheet Language XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Embed Size (px)

Citation preview

Page 1: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents
Page 2: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

XSL – Extensible Style Sheet Language XSLT – XSL Transformations

› Used to transform XML documents to other formats ,like HTML or other XML documents

It uses XPATH to navigate thro XML docs

Elements and attributes can be added/removed from the document

Page 3: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

XSLT uses XPATH to define parts of the source document that should match one or more predefined templates

When a match is found ,XSLT transforms the matching part of the source document into result document

Page 4: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Create an XSL Style Sheet

xsl:stylesheet is the root element

Link the Style sheet to XML document

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

<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>”

Page 5: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Template consists of rules to apply when a matching node is found

xsl:template is used to build template The match attribute is used to associate

a template with an XML element. The value of the match attribute is an

XPath expression

Match=“/” is defines the whole document

<xsl:template match="/“>

Page 6: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

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

<?xml-stylesheet type="text/xsl" href=“name.xsl"?>

<program><btech>    <name>Arun</name>    <regno>10BIT001</regno> <cgpa>9</cgpa></btech>

</program>

<?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>Students Name List</h2>  <table border=‘1’>    <tr bgcolor=‘white’>      <th>Name</th>      <th>Register No</th>    </tr>    <xsl:for-each select=“program/btech”>    <tr>     <td><xsl:value-of select=name"/></td>     <td><xsl:value-of select=“regno"/></td>    </tr>    </xsl:for-each>  </table>  </body>  </html></xsl:template></xsl:stylesheet>

Save as Program.xml (top)Save as Name.xsl (right)

Open the program.xml file in a browser

Page 7: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Used to extract the value of a selected node

<td><xsl:value-of select=name"/></td>     <td><xsl:value-of select=“regno"/></td>

Page 8: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Used for looping through XML document

<xsl:for-each select=“program/btech”>    <tr>     <td><xsl:value-of select=name"/></td>     <td><xsl:value-of select=“regno"/></td>    </tr>    </xsl:for-each>

Page 9: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Used for conditional testing against the content of the XML document

<xsl:if test=“expression”> <!--content goes here-->

</xsl:if>

<xsl:for-each select=“program/btech”><xsl:if test=“cgpa &gt; 9“>          <tr>     <td><xsl:value-of select=name"/></td>     <td><xsl:value-of select=“regno"/></td>    </tr></xsl:if></xsl:for-each>

Page 10: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

Used for conditional testing against the content of the XML document

<xsl:if test=“expression”> <!--content goes here-->

</xsl:if>

<xsl:for-each select=“program/btech”><xsl:if test=“cgpa &gt; 9“>          <tr>     <td><xsl:value-of select=name"/></td>     <td><xsl:value-of select=“regno"/></td>    </tr></xsl:if></xsl:for-each>

Page 11: XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents

www.w3schools.com XSLT Reference by John W. Shipman