BIT320 Texts — November 2005

November 16, 2005

XSLT for Dummies — Chapter 3

Some preliminaries explaining XSLT

Topics:

This chapter is an important overview of how stylesheets work. In particular, it provides important details about how XSLT views XML documents and how it chooses templates to apply. The advantage of using something like XSLT is that it is a general approach that is being built into most programming languages (e.g., .NET, Java, Perl, PHP). Therefore, if you learn XSLT, you are learning a generally applicable approach.

  1. Pages 32-36: This is a good reference for what we have done so far with stylesheets. In particular, the table on Page 32 provides a good summary of the elments that control stylesheet processing and output.
  2. Pages 36-38: Remember when I told you about the xsl: prefix. The section on namespaces gives the details of how that works.
  3. Pages 38-40: To use the basic features of XSLT, you can get by without understanding how XSLT represents documents. However, even in this class, we need a better understanding of how XSLT views the xml source and output it creates. These five pages provide a good introduction to XSLT's tree view of documents. We've been using the terminology. Now, you can see it graphically.

Bud last edited this November 16, 2005 | Permalink | Comments (0)

XSLT for Dummies — Chapter 4

We finally get into doing XSLT and making it work for ourselves.

Topics:

This chapter focuses on templates and template rules. These are the bread and butter of the transformations we have been talking about in class. I went through from page 50 on and created stylesheets for each of the examples. You should run these interactively as you read the material. You will note, on occasion, that the output shown in the book is sometimes just a subset of the output you get when you apply the stylesheet to the xml file. In these cases, the book has removed the extra output to reduce clutter. However, you should ask yourselves why the extra output occurs.

The examples are available from the class web site as XSLTfdCh04.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 XSLTfdCh04 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 54: Why does basic.p54-2.xsl produce the output it produces when applied to miniscore.xml? Why do I not just get output for the film elemen? Hint: When is this template applied in processing? Are any default rules being applied (described fully on pages 58-62)?
  2. Page 55: Why does basic.p55-2.xsl produce the output it produces when applied to miniscore.xml? Why do I not just get output for the composer element? Hint: This is just like the question above.
  3. Page 56: Why doesn't basic.p56-1.xsl produce extra output (i.e., the text value of the nodes that do not have specific template rules) when applied to miniscore.xml? Hint: What element is being matched by the template rule? If a template rule matchtes a node, it takes over processing for that node and all of its children. If xsl:apply-templates is not part of the matching template, the default rules will not apply.
  4. Pages 58-60: Here the built-in template rules are described in detail.
  5. Page 61: Pay close attention to these matching rules. The author does not discuss modes as we have in class. All a mode does is say that a template rule belongs to a set. When you apply template rules of a particular mode, you are just saying to apply nodes from a particular set. Templates with a mode attribute only match when their mode is the one requested in xsl:apply templates.
  6. Page 63: Finally, some discussion of named templates. Note how the author uses them; very similar to what we have seen in class.

Bud last edited this November 16, 2005 | Permalink | Comments (0)

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 last edited this November 20, 2005 | Permalink | Comments (0)

November 28, 2005

XSLT for Dummies — Chapter 6

You should be putting it all together in this chapter.

Topics:

This chapter brings together the following elements:

  1. The XSLT algorithm for applying template rules to the source xml document (Chapter 3).
  2. Template rules themselves (Chapter 4).
  3. XPath for determining which elements in the source document that template rules apply to (Chapter 5).

These elements should be coming together for you also. In particular, you should understand how XSLT applies templates to the source document. This chapter makes extensive use of empty templates to suppress processing of parts of the source document. It also uses templates that match specific elements to perform special processing on those while using the identity transformation (XSQL1) to copy all of the other nodes in the source document. You are in measure now to understand how these strategies work. Study them here.

The author created examples, but some of them are slightly broken or miss-labeled. I also added some top level elements to the stylesheets to control output. I placed my repaired stylesheets in XSLTfdCh06.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 XSLTfdCh06 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.

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 94: You should have copies of coffee.xml and coffee-light.xml in front of you. Now examing the template rule on this page. Which template rule is matched first? Why? How does that template rule control processing so that only prices are processed? How does the second template rule make it so that only price data is output? Here are my answers. The second template rule is matched first. In processing the source document, coffee.xml, the xslt parser (or processor) encounters coffees as the root element. It lools for a template rule that can match it. Well, there is a template rule that has "coffees" exactly as its match attribute. That is as specific as you can get, so it matches, and the rule is applied. The matched template rule has one xsl instruction in it xsl:apply-templates. This instruciton says to select all nodes on the XPath region/coffee/price and find templates that match them. Well, there is one template rule that can match these elements and it has a match attribute of "price". That rule says to apply-templates to all of the child nodes of price. Price's only child is its value. There are no specific templates that match this node, so the value is output based on the default template rule.
    Why aren't all the other elements also processed by the default template rules? When the root element coffees was matched by a template, processing for the whole document passed to that template. That template said to only select elements matching the XPath expression contained in its select attribute. Therefore, nothing else was processed.
    Read and re-read this explanation if you do not get it. If it is still murky, ask me.
  2. Page 96: How does changing xsl:apply-templates to xsl:copy-of alter the processing of the first template compared with the first template in the stylesheet on Page 94? Note that xsl:copy-of is very similar to the Identity Transformation I showed you in XSQL1. The difference is that the xsl:copy-of works all at once while the Identity Transformation individually matches all nodes in the source tree and copies them. With the Identity Transformation, you can alter how individual nodes or groups of nodes are processed by using specific template rules while still copying everything that does not match those rules. That is not possible with xsl:copy-of.
  3. Page 100: Behold the identity transformation and just the case I was talking about. The author wants to do something special with the coffee element and copy all the rest. Questions you should be able to answer: 1) Which template is applied first? 2) When is the template that matches coffee applied? 3) Which template matches when the "coffee" template calls xsl:apply-templates?
  4. Page 107: The stylesheet on this page is called coffee-remove_attr.xsl.
  5. Page 108: This stylesheet is called coffee-remove_attr2.xsl. How does the template that matches region avoid matching attributes? Hint, what subset of nodes is specified by the xsl:apply-templates' select attribute in this template? How can you tell that attributes are not part of the subset?
  6. Page 109: The stylesheet I supplied adds a root element so that you can use it with coffee.xml.
  7. Page 110: There is a typo in this template. The xsl:value-of that occurs in the bestwith literal result element should select "bestwith" and not "taste".
  8. Page 113: Why doesn't this stylesheet copy the region element to the output? Hint: Pay careful attention to the match attributes on the template rules.
  9. Page 116: Note the empty template rules. You should be able to explain how they suppress the copying of the source elements they match to the output. What would happen if these empty template rules were not here?

Bud last edited this November 28, 2005 | Permalink | Comments (0)

XSLT for Dummies — Chapter 7

This is just sort of catching up for a few conditional expressions.

Topics:

This chapter illustrates the use of conditional and looping statements. The author makes the point that most of the time you can just write a new template rule instead of using one of these constructs. That is true, and the create-a-template strategy is preferrable in XSLT. You are taking advantage of its built-in processing algorithm to take care of conditions.

However, there are times when using a conditional statement is required. Recall customerHTMLnoRows.xsl from XSQL2. Our strategy there was to call a named template to create a table. That way, we gained a re-usable piece of code that was not tied to the specific way we named our elements in a particular document. However, we needed a way to figure out when to call the named template. That's where the conditional xsl:choose came in to play.

The author created examples, but some of them are slightly broken or miss-labeled. I also added some top level elements to the stylesheets to control output. I placed my repaired stylesheets in XSLTfdCh07.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 XSLTfdCh07 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.

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. Pages 126-127: There are two things here. First, you can test if nodes exist exist by writing an XPath expression. If the expresesion results in a non-empty node set (i.e., there are nodes that match the XPath expression), then the condition is true. Second, you can perform comparisons between the value of a node and some standard. If the comparison is true, the condition is true (this is how we usually think of conditional statements).
  2. Page 131: xsl:choose allows you to use multiple conditions and allows for an otherwise possibility. xsl:if only allows the testing of one condition.
  3. Page 135: We used xsl:for-each in the template rule CreateSimpleTable in simpleTable.xsl. xsl:for-each allows you to process a number of nodes within a template and then perhaps process those nodes differently again within the same template. For instance, we created a header row for our table using the first row of database results. We then re-processed all of the rows of database results, including the first row, to create the data rows.

Bud last edited this November 28, 2005 | Permalink | Comments (0)