• Like
  • Save
Week 12 xml and xsl
Upcoming SlideShare
Loading in...5
×
 

Week 12 xml and xsl

on

  • 1,749 views

This is based on the w3schools xml and xsl tutorial

This is based on the w3schools xml and xsl tutorial

Statistics

Views

Total Views
1,749
Views on SlideShare
1,319
Embed Views
430

Actions

Likes
0
Downloads
26
Comments
0

2 Embeds 430

http://www.urshula.com 428
http://webcache.googleusercontent.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Week 12 xml and xsl Week 12 xml and xsl Presentation Transcript

    • Week 12 XML and XSL Revision :
    • XSLT (Extensible Stylesheet Language Transformations)
      • is a   declarative,   XML-based language used for the   transformation   of XML documents. The original document is not changed; rather, a new document is created based on the content of an existing one .
    • Overview
      • The XSLT processing model involves:
      • one or more XML  source  documents;
      • one or more XSLT  stylesheet  modules;
      • the XSLT template processing engine the  processor ); and
      • one or more  result  documents.
      • The XSLT processor ordinarily takes two input documents -an XML source document, and an XSLT stylesheet—and produces an output document. The XSLT stylesheet contains a collection of  template rules :  instructions  and other directives that guide the processor in the production of the output document.
    • XSL Syntax revision
      • You must include <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> as the first line of code. (The encoding attribute is optional when using XML Version 1.0)
    • You must then use the Correct Style Sheet Declaration
      • The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform> (The exact line below must be used)
      • <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;>
      • The lines above do not only declare that the document we have created is an XSL style sheet but also that the XSLT namespace is being used which gives us access to the XSLT elements, attributes and features. (nothing in this line optional)
      • The xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version=&quot;1.0&quot;
    • Link the XSL Style Sheet to the XML Document
      • To link a XSL style sheet reference to your XML document you just need to add the line underlined below to your XML document:
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;cdcatalog.xsl&quot;?> <catalog>  etc etc
      • </catalog>
    • XSLT    Elements
    • The <xsl:template> Element
      • The   match   attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match=&quot;/&quot; defines the whole document).
      Node Description/ attributes Example xsl:template Specifies processing templates “ match” is when the template should be used. “name” gives the template a name which xsl:call-templates etc can be use to call this template. <xsl:template match=&quot;/input&quot;> … </xsl:template>
    • The <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. The   value-of element often uses the select   attribute. The example from last week contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories
      This element generally uses the select attributes to specify the string to output. This element is self closing. <xsl:value-of select=&quot;catalog/cd/title&quot;/> Node Description/ attributes Example xsl:value-of The <xsl:value-of> element is used to extract the value of a selected node. This element generally uses the select attributes to specify the string to output. This element is self closing. <xsl:value-of select=&quot;catalog/cd/title&quot;/>
    • The <xsl:for-each> Element
      • The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set. It can be used   to loop through the XML elements, and display all of the records or specific records. The   select   attribute in the example from last week contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories.
      Filtering the Output We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element. Node Description/ attributes Example xsl:for-each Creates a loop which repeats for every match “ select” designates the match criteria Must be closed off <xsl:for-each select=&quot;catalog/cd&quot; > … </xsl:for-each>
    • Filtering the Output
      • We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.
      • <xsl:for-each select=&quot; catalog/cd[artist='Bob Dylan']&quot;>
      • Legal filter operators are:
      • =  (equal)
      • != (not equal)
      • &lt; less than
      • &gt; greater than
    • Take a look at this modified example XSL style sheet:
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;>   <html>   <body>   <h2>My CD Collection</h2>   <table border=&quot;1&quot;>     <tr bgcolor=&quot;#9acd32&quot;>       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select=&quot;catalog/cd[artist='Bob Dylan']&quot;>     <tr>       <td><xsl:value-of select=&quot;title&quot;/></td>       <td><xsl:value-of select=&quot;artist&quot;/></td>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>
    • Exercise 2
      • Open your xml and xsl documents from last week and modify the xsl document so all teachers except me are displayed
    • XSLT   <xsl:sort>   Element
      • The <xsl:sort> element is used to sort the output. Note:   It is possible for two conforming XSLT processors not to sort exactly the same
      Node Description/ attributes Example xsl:sort The sort element is used to sort the output by a particular element in ascending order. It can be a self contained element so there is no closing tag. It has the following attributes   order=&quot;ascending&quot; | &quot; descending&quot;  select=&quot;expression&quot; data-type=&quot;number&quot; &quot;qname&quot; | &quot;text Can be included inside the xsl:for-each and xsl:template elements <xsl:sort />
    • To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;>   <html>   <body>   <h2>My CD Collection</h2>   <table border=&quot;1&quot;>     <tr bgcolor=&quot;#9acd32&quot;>       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select=&quot;catalog/cd&quot;>       <xsl:sort select=&quot;artist&quot;/>       <tr>         <td><xsl:value-of select=&quot;title&quot;/></td>         <td><xsl:value-of select=&quot;artist&quot;/></td>       </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>
    • Exercise 3
      • Open your xml and xsl documents from last week and modify the xsl document so the teachers are sorted by their first names in descending order
    • XSLT   <xsl:if>   Element
      • To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document. Note:   The value of the required   test   attribute contains the expression to be evaluated
      • Syntax
      • <xsl:if test=&quot; expression &quot;>   ...some output if the expression is true... </xsl:if>
      Node Description/ attributes Example xsl:if Yes or No conditions “ test” specifies criteria for entering the if To add a conditional test, add the <xsl:if> element inside the <xsl:for-each> element in the XSL file: <xsl:if test=&quot;price &gt; 10&quot;> … </xsl:if>
    • Example
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;>   <html>   <body>   <h2>My CD Collection</h2>   <table border=&quot;1&quot;>     <tr bgcolor=&quot;#9acd32&quot;>       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select=&quot;catalog/cd&quot;>       <xsl:if test=&quot;price &gt; 10&quot;>         <tr>           <td><xsl:value-of select=&quot;title&quot;/></td>           <td><xsl:value-of select=&quot;artist&quot;/></td>         </tr>       </xsl:if>     </xsl:for-each>   </table>   </body>   </html> </xsl:template>
      • </stylesheet>
    • XSLT   <xsl:choose>   ,<xsl:when> and <xsl:otherwise> Elements
      • 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=&quot; expression &quot;>     ... some output ...   </xsl:when>   <xsl:otherwise>     ... some output ....   </xsl:otherwise> </xsl:choose>
    • the   <xsl:choose>   Elements Node Description/ attributes Example xsl:choose Multiple choices No attributes <xsl:choose>   <xsl:when test=&quot; expression &quot;>     ... some output ...   </xsl:when>   <xsl:otherwise>     ... some output ....   </xsl:otherwise> </xsl:choose> xsl:when Yes or No conditions “ test” specifies criteria for entering the if <xsl:when test=&quot;$type='radio'&quot;> … </xsl:when> xsl:otherwise The default choice if none of the “xsl:when” criteria are met <xsl:otherwise> … </xsl:otherwise>
    • Where to put the Choose Condition
      • To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
      • Example
      • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;>   <html>   <body>   <h2>My CD Collection</h2>   <table border=&quot;1&quot;>     <tr bgcolor=&quot;#9acd32&quot;>       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select=&quot;catalog/cd&quot;>     <tr>       <td><xsl:value-of select=&quot;title&quot;/></td>        <xsl:choose>         <xsl:when test=&quot;price &gt; 10&quot;>           <td bgcolor=&quot;#ff00ff&quot;>           <xsl:value-of select=&quot;artist&quot;/></td>         </xsl:when>         <xsl:otherwise>           <td><xsl:value-of select=&quot;artist&quot;/></td>         </xsl:otherwise>        </xsl:choose>     </tr>      </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>
    • Exercise 4
      • Open your xml and xsl documents from last week and modify the xsl document so that each teacher’s data is in a different colour