22
Week 12 XML and XSL Revision :

Week 12 xml and xsl

  • Upload
    hapy

  • View
    1.824

  • Download
    2

Embed Size (px)

DESCRIPTION

This is based on the w3schools xml and xsl tutorial

Citation preview

Page 1: Week 12 xml and xsl

Week 12 XML and XSL

Revision :

Page 2: Week 12 xml and xsl

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.

Page 3: Week 12 xml and xsl

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

Page 4: Week 12 xml and xsl

XSL Syntax revision

You must include

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

as the first line of code. (The encoding attribute is optional when using XML Version 1.0)

Page 5: Week 12 xml and xsl

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

• 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="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0"

Page 6: Week 12 xml and xsl

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="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?><catalog> etc etc

• </catalog>

Page 7: Week 12 xml and xsl

XSLT  Elements

Page 8: Week 12 xml and xsl

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="/" 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="/input">…</xsl:template>

Page 9: Week 12 xml and xsl

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

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="catalog/cd/title"/>

This element generally uses the select attributes to specify the string to output. This element is self closing.

<xsl:value-of select="catalog/cd/title"/>

Page 10: Week 12 xml and xsl

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.

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="catalog/cd">

…</xsl:for-each>

Filtering the OutputWe can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.

Page 11: Week 12 xml and xsl

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="catalog/cd[artist='Bob Dylan']">

• Legal filter operators are:• =  (equal)• != (not equal)• &lt; less than• &gt; greater than

Page 12: Week 12 xml and xsl

Take a look at this modified example XSL style sheet:

• <?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>  <table border="1">    <tr bgcolor="#9acd32">      <th>Title</th>      <th>Artist</th>    </tr>    <xsl:for-each select="catalog/cd[artist='Bob Dylan']">    <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 13: Week 12 xml and xsl

Exercise 2

• Open your xml and xsl documents from last week and modify the xsl document so all teachers except me are displayed

Page 14: Week 12 xml and xsl

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="ascending" | " descending" select="expression"

data-type="number" "qname" | "text

Can be included inside the xsl:for-each and xsl:template elements

<xsl:sort />

Page 15: Week 12 xml and xsl

To sort the output, simply add an <xsl:sort> element inside the <xsl:for-

each> element in the XSL file: • <?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>  <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>  </body>  </html></xsl:template></xsl:stylesheet>

Page 16: Week 12 xml and xsl

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

Page 17: Week 12 xml and xsl

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="expression">  ...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="price &gt; 10">…</xsl:if>

Page 18: Week 12 xml and xsl

Example• <?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>  <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>  </body>  </html></xsl:template>

• </stylesheet>

Page 19: Week 12 xml and xsl

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.

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

Syntax

Page 20: Week 12 xml and xsl

the <xsl:choose>  Elements

Node Description/ attributes Example

xsl:choose Multiple choicesNo attributes

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

xsl:whenYes or No conditions“test” specifies criteria for entering the if

<xsl:when test="$type='radio'">…</xsl:when>

xsl:otherwiseThe default choice if none of the “xsl:when” criteria are met

<xsl:otherwise>…</xsl:otherwise>

Page 21: Week 12 xml and xsl

Where to put the Choose ConditionTo 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="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>  <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>  </body>  </html></xsl:template></xsl:stylesheet>

Page 22: Week 12 xml and xsl

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