BIT320 Texts

November 21, 2005

XSLT for Dummies — Chapter 5

We are introduced to XPath, the standard that makes matching in XSLT go.

Topics:

This chapter focuses on XPath. XPath is used all-over in XSLT. It is a method for locating nodes and attributes based on their position in the XML document. This part of the XSLT specification is becoming widely adopted and is very widely used, even for non-XML applications. If you take XPath step-by-step, it is not so bad. If it seems a little overwhelming (which it can the first time through), focus on the child, attribute, self, and sibling axes. They are the ones we will use the most.

As with Chapter 4, I went through from page 73 on and created stylesheets for each of the examples (the author does not provide them until Chapter 6). You should run these interactively as you read the material. Unlike the last chapter, I tried to remove extraneous output in these examples. I did so by providing empty template rules for elements I did not want to copy to the output. This deleted both the element and its children. Why? As XSLT processes the source document, it starts at the root and then descends down each branch of the tree until it runs out of branches. Every time, it encounters a node that matches a template rule, it passes of processing of that node and its children to the template rule. Therefore, if, as is the case with empty template rules, the template does not specify any output, the node and its children are effectively deleted.

The examples I created are available from the class web site as XSLTfdCh05.zip under In Class for the day this chapter is assigned. You should transfer them to elab-linux4 and unzip them in a directory, for instance XSLTfdCh05 under webapps. To apply a stylesheet to an xml file, you should type oraxsl file.xml stylesheet.xsl where file.xml is the name of the xml file and stylesheet.xsl is the name of the stylesheet.

Here's how I made the stylesheets. Each time there was an xsl:template or xsl:styleheet example on a page, I constructed a stylesheet to illustrate it. The stylesheets are named according to the pattern basic.p??-#.xsl where ?? indicates the page number and # indicates which example it was on that page. Pages had up to four examples. So, for instance, to apply the stylesheet basic.p50-1.xsl to tv.xml, you would type oraxsl tv.xml basic.p50-1.xsl at the command line in the directory where you have both files on elab-linux4.

If you interactively do the examples as you go, you will gain a very good working knowledge of XSLT. Follows notes on specific elements:

  1. Page 73: Note that my stylesheet has a few extra elements in it. They are:
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    at the head of the document and
    <xsl:text>&#xa;</xsl:text>
    
    in the middle of the document. We have seen the xsl:output element before. The xsl:strip-space element says to delete any extraneous spaces, tabs, or carriage returns from the output. It helps make the formatting look nice. The code &#xa; in the xsl:text element outputs a carriage return. All of these little pieces were added just to make things look better.

Bud edited this on November 20, 2005

Comments

Post a comment




Remember Me?