1 / 16

XSLT

XSLT. Contents . XSLT and uses XSLT example Simple XML How XSLT works? Template-match Value-of For-each and sort If Choose-when Apply-templates Where XSLT can be used?. XSLT and Uses.

eydie
Download Presentation

XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XSLT

  2. Contents • XSLT and uses • XSLT example • Simple XML • How XSLT works? • Template-match • Value-of • For-each and sort • If • Choose-when • Apply-templates • Where XSLT can be used?

  3. XSLT and Uses • XSLT stands for Extensible Stylesheet Language Transformations. XSLT is designed for use as part of XSL, which is a stylesheet language for XML - Transforms one XML to another XML or other text such as HTML • Advantages: • Easy to merge data into XML data into a presentation • XSLT, when used in websites, allow strict separation between HTML and code-behind. • We use XSLT extensively for things like documentation, and making some complex configuration settings user-serviceable

  4. XSLT example

  5. SpecifyXSL for display Sample XML <?xml:stylesheet type="text/xsl" href="classical.xsl"?> <authors> <author period="Classical"> <name>Mozart</name> <nationality>Austrian</nationality> </author> <author period="Classical"> <name>Beethoven</name> <nationality>German</nationality> </author> <author period="Baroque"> <name>Bach</name> <nationality>German</nationality> </author> </authors>

  6. XSL version XPath Sample XSL <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD><TITLE>Authors</TITLE></HEAD> <BODY> <H1>Composers from Austria</H1> <TABLE BORDER="1"><TR><TH>Name</TH></TR> <xsl:for-each select="/authors/author[nationality='Austrian']"> <TR><TD><xsl:value-of select="name" /></TD></TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

  7. Putting it together • The XSL was: <xsl:template match="/"> <html><body><h1> <xsl:value-of select="message"/> </h1> </body></html></xsl:template> • The <xsl:template match="/"> chooses the root • The <html><body> <h1> is written to the output file • The contents of message is written to the output file • The </h1> </body></html> is written to the output file • The resultant file looks like: <html><body> <h1> Hiiii</h1></body></html>

  8. How XSLT works • The XML text document is read in and stored as a tree of nodes • The <xsl:template match="/"> template is used to select the entire tree • The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree • Unmatched parts of the XML tree are not changed • After the template is applied, the tree is written out again as a text document

  9. Template match <xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <tr>      <td><xsl:value-of select="catalog/cd/title"/></td>      <td><xsl:value-of select="catalog/cd/artist"/></td>    </tr>  </table>  </body>  </html></xsl:template>

  10. Value-of <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>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>

  11. For-each and sort <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr> <xsl:for-each select="catalog/cd">      <xsl:sort select="artist"/>      <tr>        <td><xsl:value-of select="title"/></td>        <td><xsl:value-of select="artist"/></td>      </tr></xsl:for-each>  </table>

  12. if <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd"><xsl:if test="price &gt; 10">        <tr>          <td><xsl:value-of select="title"/></td>          <td><xsl:value-of select="artist"/></td>        </tr></xsl:if>    </xsl:for-each>  </table>

  13. Choose when <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd">    <tr>      <td><xsl:value-of select="title"/></td><xsl:choose>        <xsl:when test="price &gt; 10">          <td bgcolor="#ff00ff">          <xsl:value-of select="artist"/></td></xsl:when>        <xsl:otherwise>          <td><xsl:value-of select="artist"/></td></xsl:otherwise> </xsl:choose>    </tr>    </xsl:for-each>  </table>

  14. Apply-templates <xsl:template match="/">  <html>  <body>  <h2>My CD Collection</h2><xsl:apply-templates/>  </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>

  15. Where XSLT can be used • With an appropriate program, such as Xerces, XSLT can be used to read and write files • A server can use XSLT to change XML files into HTML files before sending them to the client • A modern browser can use XSLT to change XML into HTML on the client side • Some applications which store data in XML use XSLT to present this data in a human-readable format. For example, Windows Live Messenger stores the trace of messages as XML, but when you open the history in WLM itself, it shows you a pretty table which in fact is HTML built through XSLT

  16. THANK YOU

More Related