Note 9 for XML

Watch & Read:

Review XPath and try with this site (give me the path to ingredient, currency, product_code & name)

XSL is the marriage between HTML and XML to create a document that is viewable by us mere mortals.

The W3 Schools said it best about :

XSL is a language that can transform, filter and sort XML data into HTML or prepare the data for different devices, like screen, paper or voice.

A common way to describe the transformation process is to say that XSL uses XSLT to transform an XML source tree into an XML result tree (the finished product)

How does it work?

In the transformation process, XSLT uses XPath to define parts of the source document that match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document. The parts of the source document that do not match a template will (as a general rule) end up unmodified in theresult.

Syntax for calling an XSL document:
<?xml-stylesheet type="text/xsl" href="URL-of-XSL-document"?>

XSL is an XML document!

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

Formating of XSL document (Template)

</xsl:template>
< /xsl:stylesheet>

 

Terms: Source Tree (Before Transformation) ------> Result Tree (After Transformation)

 

Try this for an example:

I want to take by XML version of my fall schedule and display only the class number. I can use an XSL template to display only the information I want!

Template creation:

The first step I used was to identify the information I wanted and construct a path

<xsl:for-each select="FallSchedule/Class">

to that information. The xsl:for-each creates a loop that continues until you end the XML document.

The second step was creating a formula to display the information I wanted

<xsl:value-of select="Number" />

I wanted to display only the information contained by the Number element!

The final step was to add HTML tags to the XSL to display (render) the information I selected.

A more comples version of XSL

Lets Take BoardGuaranteed.com' RSS and make our own version of an RSS reader. We will pull title, description and link and put it in a table.

 

Review the following: The following two tables were borrowed from W3schools What a Great Web Site for !

 

Terms you need to Lookup:

Element Name Description Attributes
xsl:apply-templates Applies a template to the current element. order-by="+|-pattern"
select="pattern"
xsl:attribute Adds a new attribute to the current output element. name="attribute-name"
xsl:cdata Adds a new CDATA section to the output.  
xsl:choose Provides a selection mechanism based on conditions.  
xsl:comment Adds a comment node to the output.   
xsl:copy Copies the current node to the output.  
xsl:define-template-set Defines a new set of templates  
xsl:element Adds a new element node to the output. name="name"
xsl:entity-ref Adds a new entity reference node to the output. name="name"
xsl:eval Provides an evaluation mechanism to evaluate output content. language="language"
xsl:for-each Provides a mechanism to create a loop in the output stream. select="pattern"
order-by="-|+ pattern"
xsl:if Provides a conditional branch mechanism based on a condition. match="pattern"
xsl:node-name Adds the name of the current node to the output.  
xsl:otherwise Part of the choose mechanism (see xsl:choose).  
xsl:pi Adds a processing instruction to the output. name="name"
xsl:script Defines a script area inside a template. language="language"
xsl:stylesheet Defines the root element of the style sheet. xmlns:xml="namespace"
language="language"
indent-result="yes|no"
xsl:template Defines a template. match="pattern"
language="language"
xsl:value-of Defines a node to insert into the output. select="pattern"
xsl:when Part of the choose mechanism (see xsl:choose) test="expression"

 

Homework:

Add XSL document to each project!

You should be reading Session 7

BoardGuaranteed-RSS Modified

Read: Chap 2 & 3

Final Step:

Review in your mind everything we covered in class and remember to cover it again before you go to bed so your mind to process this information while you sleep.