WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (9/13)
[previous] [next] |
XML for ASP.NET Developers
Accessing "Return" Values of XSLT Templates
You may have noticed that although a template can act somewhat like a method,
it has no way to return a response...or does it? By wrapping the
xsl:variable
element around a template call made using the
xsl:call-template
element, the output normally written to the result
tree can instead be captured by the variable. This process is shown next:
<xsl:template match="Customer">
<xsl:variable name="CID" select="@CustomerID"/>
<xsl:variable name="Orders">
<xsl:call-template name="GetNames">
<xsl:with-param name="CustomerID" select="$CID"/>
</xsl:call-template>
</xsl:variable>
<b>Customer</b><br/>
ID: <xsl:value-of select="$CID"/><br/>
<xsl:for-each select="$Orders//Order"/>
<xsl:value-of select="@OrderID"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="GetNames">
<xsl:param name="CustomerID" select="'ALFKI'"/>
<xsl:value-of select="//Orders[@CustomerID = $CustomerID]"/>
</xsl:template>
The variable named Orders
will be filled with a node-set generated
by a call to the GetNames
template. Doing this offers a powerful means
for building more dynamic and efficient XSLT documents.
Now that you're familiar with several of the main elements used in creating XSLT style sheets, it's time to examine XSLT functions.
XSLT Functions
In Chapter 3, "XPath, XPointer, and XLink," you were introduced to several functions built in to the XPath language. Because XSLT relies on XPath for creating expressions that locate nodes in an XML document, these functions are available for use in your XSLT documents. In addition to these functions, the XSLT language adds a few more. Table 7.3 shows these functions and provides a description and example of using them in XSLT documents.
Table 7.3 XSLT Functions
Function |
Description |
|
Returns a node set that contains the current node as its only member.
This function exists to help identify the current node when it is different
from the context node. In previous examples you have seen that the context
node can be represented by the
|
|
This code will provide the same result:
|
|
At this point in the template, the current node is the same as the context node. When used within the square brackets of a predicate ([ and ]), however, the current node is often different from the context node. An example of this is shown next: |
|
|
|
The predicate statement ( |
|
Although XSLT makes it easy to transform a single XML document into
other structures, what happens if the result tree needs to be created
from more than just one XML document? Using the
|
|
This will load
|
|
Although the preceding |
|
Aside from providing a string URI value, a node-set can be passed to access the remote document, as shown next: |
|
For the XML document:
|
|
The following XSLT document creates a result tree containing the handicap nodes from the referenced documents:
|
|
This function is useful if you are writing an XSLT document that may be processed using different XSLT processors. Before using elements that you know may not be supported, a check can be made to see if the processor does indeed support the element in question. This is helpful when testing for XSLT elements found in a later version of XSLT or in checking for vendor specific elements. The function will return a Boolean value:
|
format-number( |
This function converts numbers to a string using a format pattern supplied in the second parameter. Here are some examples of using this function: |
|
The following function call returns format-number(5351,"#,###")
|
|
The following function call returns format-number(5351, "#.00")
|
|
The following function call returns format-number(53.51, "#.0000")
|
|
The following function call returns format-number(53.51, "0000.0000")
|
|
The following function call returns format-number(53.51, "0000.####")
|
|
The following function call returns format-number(53.56, "0.0")
|
function-available(string)
|
Similar to element-available, although this function checks whether specific functions are supported by the XSLT processor. |
|
This function generates a string that is guaranteed to uniquely identify a node. The same string will always be returned for the same node. The string that is generated will vary from processor to processor and will start with an alphabetic character. |
|
|
key(string,object)
|
This function is used in conjunction with the
|
|
This key can then be accessed by using the key() function:
|
system-property(string)
|
This function returns the value of the system property identified by
the name passed as the argument. Three different system properties must
be supported by a compliant XSLT processor, including
|
unparsed-entity-uri()
|
This function returns declarations of unparsed entities in the DTD of the source XML document. |
|
Given the entity declaration:
|
|
The following code: <xsl:value-of select="unparsed-entity-uri('clubs')"/>
|
|
Would return a value of |
[previous] [next] |
© Copyright Pearson Education and
Created: April 22, 2002
Revised: April 22, 2002
URL: https://webreference.com/authoring/languages/xml/aspnet/chap7/9.html