Conditional Processing


XSL Transformations have the capability to make decisions at run-time (that is, during processing of the source tree). They have the equivalent of the if statement and also the equivalent of the switch statement as used in other languages. Both of these elements perform a test on an expression and proceed accordingly .

These elements use XPath functions to perform their tests, which we have not covered and will not cover in detail. I'll explain what each of the functions does when it is used. A list of the XPath functions supported by Microsoft can be found in Appendix C.

The <xsl:if> Element

The <xsl:if> element performs a Boolean test, as illustrated in the following example, that does one of two possible actions.

 <!-- Category: instruction -->  <xsl:if    test = boolean-expression>    <!-- Content: template -->  </xsl:if> 

Here's an example that follows the syntax in the preceding example. The position() function and what this code accomplishes are explained in the sidebar.

 <xsl:template match="NAME">    <xsl:value-of select="FIRST"/>    <xsl:value-of select="LAST"/>    <xsl:if test="position() != last()">, </xsl:if>  </xsl:template> 

The position() function returns the value of the position of the context element. During processing, each node is given a position value. The first element starts at 1. The last() function returns the number of the last node in the tree. In this example, these functions tell the processor when the last node is being processed so that a comma will not be added to the end of the list.

After each first and last name combination is output, a comma and space are added. The result is a comma-separated list of names like this:

Scott Fitchet, Stephanie Wall, Rubi Olis, John Griffin

The <xsl:choose> Element

The <xsl:choose> element, as illustrated in Listing 2.22, selects between multiple choices. If there are only two items from which to choose, it performs the equivalent of the IF-THEN-ELSE statement. If there are more than two alternatives, it performs the equivalent of the switch or select statement used in other languages.

Listing 2.22 Syntax for the <xsl:choose > Element
 <!-- Category: instruction -->  <xsl:choose>    <!-- Content: (xsl:when+, xsl:otherwise?) -->  <xsl:when    test = boolean-expression>    <!-- Content: template -->  </xsl:when>  <xsl:otherwise>    <!-- Content: template -->  </xsl:otherwise>  </xsl:choose> 

The <xsl:choose> element also has two other elements associated with it that allow it to perform its function.

The <xsl:when> Element

Each <xsl:when> element has a single attribute, test , that is identical to the test attribute of the <xsl:if> element. The content of the <xsl:when> element is a tem-plate.When an <xsl:choose> element is processed, each of the <xsl:when> elements is tested in turn by converting the expression to a Boolean value. The content of the first, and only the first, <xsl:when> element whose test is true is used.

The <xsl:otherwise> Element

Each <xsl:choose> element has an optional <xsl:otherwise> element. If no <xsl:when> is true, the content of the <xsl:otherwise> element is used. If no <xsl:when> element is true and no <xsl:otherwise> element is present, no output is generated.

Looping

The <xsl:for-each> element processes a set of nodes selected by an XPath expression and performs the same processing on each of these nodes. The select attribute is required and contains the XPath expression, which selects the node set to process. The following example illustrates the syntax for the <xsl:for-each > element

 <!-- Category: instruction -->  <xsl:for-each>  select  =  node-set-expression  >    <!-- Content: (  template  ) -->  </xsl:for-each> 

For example, take an XML document with the structure illustrated in Listing 2.23:

Listing 2.23 XML Document Structure
 <resumes>    <person>      <name>        <first>...</first>        <last>...</last>  ... </name>    </person>    <person>      <name>      <first>...</first>      <last>...</last>  ...</name>    </person>  </resumes> 

The example shown in Listing 2.24 would create an HTML document containing a table with a row for each person element.

Listing 2.24 Resulting HTML Document
 <xsl:template match="/">    <html>      <head>        <title>Resumes</title>      </head>      <body>        <table>        <tbody>        <xsl:for-each select="resumes/person">          <tr>            <th>              <xsl:apply-templates select="name"/>            </th>            <td>              <xsl:value-of select="last"/>            </td>            <td>              <xsl:value-of select="first"/>            </td>          </tr>        </xsl:for-each>        </tbody>        </table>      </body>    </html>  </xsl:template> 

Controlling Output

An XSL stylesheet is abstractly processed in two stages. The first step creates a result tree from a source tree, and the second step generates serialized output of this result tree. The <xsl:output> element governs this second step. This process isn't required by the specification; in fact, if a processor doesn't provide output in serial format, this element is ignored. Listing 2.25 shows an example.

Listing 2.25 Syntax for the <xsl:output > Element
 <!-- Category: top-level-element -->  <xsl:output    method = "xml"  "html"  "text"  qname-but-not-ncname    version = nmtoken    encoding = string    omit-xml-declaration = "yes"  "no"    standalone = "yes"  "no"    doctype-public = string    doctype-system = string    cdata-section-elements = qnames    indent = "yes"  "no"    media-type = string  /> 

The following sections discuss the three output formats of the <xsl:output> element.

XML Output Method

Setting the method attribute to XML forces XML output. This is also the default output format and the one invoked if the method attribute is not present. Usually, the output form will be a well- formed XML document, but it is only required to be an XML fragment that can be incorporated into another XML document with an entity reference such as "&legal_woes" .

HTML Output Method

Setting the method attribute to HTML forces HTML output. Processing instructions ( <?...?> , and so on will be filtered from the output, which is standard HTML 4.0. Entity references do not require escaping, meaning "<" will be output as "<" net "&lt;" .

Have you found yourself wondering how a stylesheet knows that the output tree it's generating is in HTML format without being told? There are three rules that must be followed:

  • There must be at least one child element.

  • The first element child of the root node must be <HTML> (dead giveaway, huh?). This is case insensitive.

  • There can be no test nodes before the <HTML> element unless they consist solely of whitespace.

Text Output Method

Setting the method attribute to text forces text output. The text output method outputs the result tree by outputting the string value of every text node in the result tree in document order without any escaping. All other nodes are ignored.

A fourth output method is provided for vendor extensions and is invoked by providing a QName for the method attribute. This method is not defined in the specification.

The <xsl:output> element is a top-level element only.

xsl:output Attributes

The other attributes on <xsl:output> provide parameters for the output method.The following attributes are allowed:

  • version . Specifies the value of the version attribute of the XML declaration that is output.

  • indent . Specifies whether the XSLT processor can add additional whitespace when outputting the result tree to make for better readability. The value must be yes or no .

  • encoding . Specifies the preferred character encoding that the XSLT processor should use to encode sequences of characters as sequences of bytes; the value of the attribute should be treated case insensitively and must contain only characters in the range #x21 to #x7E (that is, printable ASCII characters ).

  • media-type . Specifies the media type (MIME content type) of the data that results from outputting the result tree.

  • doctype-system . Specifies the system identifier for an external DTD.

  • doctype-public . Specifies the public identifier for an external DTD.

  • omit-xml-declaration . Specifies whether the XSLT processor should output an XML declaration; the value must be yes or no . This is not applicable to HTML output.

  • standalone . Specifies whether the XSLT processor should output a standalone document declaration; the value must be yes or no . This is not applicable to HTML output.

  • cdata-section-elements . Specifies a list of the names of elements whose text node children should be output using CDATA sections. This is not applicable to HTML output.

Here are some examples of the <xsl:output...> element.

Generate an XML indented document with all <SCRIPT> elements translated to "<![CDATA[]]>" sections. We also need a SYSTEM DTD declaration with a standalone attribute of no . The result shown in Listing 2.26.

Listing 2.26 Answer to the First Example
 <xsl:output    method="xml"    indent="yes"    encoding="iso-8859-1"    cdata-section-elements="script"    doctype-system="resumes.dtd"    standalone="no"  /> 

For a second example, generate a text document in ASCII format. The result is as follows:

 <xsl:output    method="text"    encoding="us-ascii"  /> 


XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net