XML and SQL Server 2000
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 .
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>
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 "<" . 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:
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.
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:
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" /> |