Part IV. Invoking the XML Document

Using XSLT to Add the XML Content

The nodes of the XML document are arranged in a hierarchal structure. Therefore, in order to call up the node content we must write the statements with attention to the node hierarchy.

Have a look at the XML document by opening it in a text editor. You may notice it consists of a root node <questionnaire> which contains the child nodes, <question> and <li>. In our sample file, there are 24 <li> strings which would require 24 rows in a table to display properly. To accomplish this, we will use two XSLT statements, the first of which is:

The <xsl:value-of> Element


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

This element calls the value of an XML node and is replaced by it in the transformation process. In this project, "expression" would be replace by the <li> node, so we would type is as, <xsl:value-of select ="li"/>. We add this to the long table cell on the bottom of the table where we want the <li> XML string to appear. This tag has no content so it uses a self-closing forward slash, similar to the break <br/> tag.

However, this element only adds the first node in the series and stops there. If there are similar nodes, they will not be referenced. For example in the project, there are 24 <li> nodes. To repeat the process and up the rest of them, an additional statement must be used: the <xsl:for-each> element.

The <xsl:for-each> Element


<xsl:for-each select="expression">

The <xsl:for-each> element is a looping statement where "expression" refers to the parent nodes expressed in a directory path format. This tag requires an appropriate closing tag </xsl:for-each> to indicate where the looping ends. In this case we would type, <xsl:for-each select="questionnaire/question"> placing its opening and closing tags before and after the single table row that contains the <xsl:value-of select ="li"/> statement we added earlier.

Because we have enclosed the row tags in the statement it not only repeats the HTML elements of the document it encloses but also calls up the contents of a different <li> node each time it loops until it completes the list.

Figure 10. The Enclosed Table Row

The statement will then replicate the content inside as many times as needed to fullfill the number of similar nodes in XML document, while replicating as many table rows needed to accomplish this.

Adding an Unordered List

You may notice in Figure 10 the unordered list tags <ul> </ul> enclosing the list tags <li> </li>. Since the unordered list tags reside between the two looping <xsl:for-each> tagset, they will be repeated along with the other elements.