Advanced XSL Syntax

Some advanced topics of XSL syntax include functions, methods , and pattern matching characters . Most of the content in this section is a little beyond the scope of this chapter but it begins to touch on facets of XSL and XSLT, such as XPath and XQuery. XPath and XQuery are more database-centric than basic XSL formatting and templating and will be covered in later chapters of this book.

Function Versus Method

One simple question should be asked: What is the difference between a function and a method?

A function is a procedural or modular programming construct describing a procedure (built into a language or custom-built), which executes as an expression. The term expression suggests that a function can not only be an expression in itself, but can also be embedded within an expression. The general technical programming definition of a function is that it returns a single value.

An expression is a calculation that returns a result. It returns a single value and something that can be used by another expression.

That single value quite often is a Boolean, but not always as functions can essentially return anything because a function is mathematically an expression. A function might look something like this:

   function ftoc(f integer) {    return (f   32) * 5 / 9; }   

The preceding script is intended to be pseudocode and not any particular programming or scripting language.

The preceding function converts temperature readings from degrees Fahrenheit into degrees Celsius and could be expressed something like this:

   The temperate in $city is ftoc($temperature).   

A function takes parameters and returns a value, and can be executed independently of the content of a structure (in the case of this book an XML document). A method is different to a function because a method operates on a specific object.

Remember from the previous chapter that object is a term that describes an iteration or copy made from a class. A class is a structure and an object is a run-time version of that class.

So where a function is called as part of an expression, a method is called as an inherent part of an object. So, a method has direct access to data. A function can be used for any appropriate data or a function does not have to access any data at all. If you use the previous temperatures example but this time with an object methodology approach, the function becomes a method defined as part of a class:

   create class CityClass {    attribute city string,    attribute fahrenheit integer,    method ftoc    {       (this.fahrenheit   32) * 5 / 9    } }   

The keyword this means the current class (object created from this class).

The method would be executed on an object iteration of the previously defined CityClass as something like the following:

   London.ftoc();   

In the preceding pseudocode snippet, London is an object created from the class called CityClass. Additionally, the method requires no input as the attribute called Fahrenheit is contained within the object itself. There is no requirement for processing that necessitates venturing outside of the object called London.

Now lets get back to XSL.

XSL Function Syntax

Functions and their syntax are as follows :

  • current : Gets the current node including all child nodes. Parsing with the "current()" function performs the same task as parsing with "." .

  • element-available and function-available : Returns true if the XSL (XSLT) element or function is available to the XSL parser in use. The following examples will both return false:

       element-available("xsl:pi") function-available("idonot-exist")   
  • format-number : Formats a number into a string display format. The following example returns 112,102.49:

       format-number(112102.4900, "#,.#")   
  • generate_id : Unique node identifier.

  • node-set : Returns a node set, which is the root node plus the specified node only (from an XML document).

  • system-property : Returns various values for various system properties (XSL or XSLT parser attributes), as extracted from processing instruction elements, such as:

       system-property("xsl:version")   

    The preceding example returns 1, which represents version 1 or the XML element with <?xml version="1.0"?>

Try It OutUsing an XSL Function
image from book

This is an XML document representing all states in the United States and provinces in Canada. Assume that the XML document is stored in a file called countries .xml :

   <?xml version="1.0" ?> <countries>    <country name="Canada">       <state name="Alberta">AB</state>       <state name="Nova Scotia">NS</state>       <state name="British Columbia">BC</state>       <state name="Quebec">QB</state>       <state name="Ontario">ON</state>    </country>    <country name="United States">       <state name="Colorado">CO</state>       <state name="Connecticut">CT</state>       <state name="Delaware">DE</state>       <state name="Florida">FL</state>       <state name="Georgia">GA</state>       <state name="Hawaii">HI</state>       <state name="Iowa">IA</state>       <state name="Idaho">ID</state>       <state name="Illinois">IL</state>       <state name="Indiana">IN</state>       <state name="Kansas">KS</state>       <state name="Kentucky">KY</state>       <state name="Louisiana">LA</state>       <state name="Massachusetts">MA</state>       <state name="Maryland">MD</state>       <state name="Maine">ME</state>       <state name="Minnesota">MN</state>       <state name="Missouri">MO</state>       <state name="Mississippi">MS</state>       <state name="Montana">MT</state>       <state name="North Carolina">NC</state>       <state name="North Dakota">ND</state>       <state name="New Hampshire">NH</state>       <state name="New Jersey">NJ</state>       <state name="Nebraska">NE</state>       <state name="New Mexico">NM</state>       <state name="Nevada">NV</state>       <state name="New York">NY</state>       <state name="Ohio">OH</state>       <state name="Oklahoma">OK</state>       <state name="Oregon">OR</state>       <state name="Pennsylvania">PA</state>       <state name="Rhode Island">RI</state>       <state name="South Carolina">SC</state>       <state name="South Dakota">SD</state>       <state name="Tennessee">TN</state>       <state name="Texas">TX</state>       <state name="Utah">UT</state>       <state name="Vermont">VEM</state>       <state name="Virginia">VA</state>       <state name="Washington">WA</state>       <state name="Wisconsin">WI</state>       <state name="West Virginia">WV</state>       <state name="Wyoming">WY</state>       <state name="California">CA</state>       <state name="Arizona">AZ</state>       <state name="Arkansas">AR</state>       <state name="Alabama">AL</state>       <state name="Alaska">AK</state>       <state name="Michigan">MI</state>    </country> </countries>   

Use an XSL function to find all the full text values contained within all the <country> nodes of the preceding XML document. This is how:

  1. Create an XSL style sheet, including appropriate HTML tags:

       <?xml version="1.0"?> <xsl:stylesheet version="1.0">    <xsl:template match="/">       <HTML><BODY>       </BODY></HTML>    </xsl:template> </xsl:stylesheet>   
  2. Use the http://www.w3.org/1999/XSL/Transform namespace:

       <?xml version="1.0"?> <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  >    <xsl:template match="/">       <HTML><BODY>       </BODY></HTML>    </xsl:template> </xsl:stylesheet>   
  3. Add an XSL for loop to parse through the <countries> collection:

       <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="/">       <HTML><BODY>  <xsl:for-each select="countries/country">   </xsl:for-each>  </BODY></HTML>    </xsl:template> </xsl:stylesheet>   
  1. Add the xsl:value-of element using the current() function to output the resulting node

       <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="/">       <HTML><BODY>          <xsl:for-each select="countries/country">  <xsl:value-of select="current()"/>  </xsl:for-each>       </BODY></HTML>    </xsl:template> </xsl:stylesheet>   
  2. Add text and a <BR/> tag into the code to indicate which node is being output:

       <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="/">       <HTML><BODY>          <xsl:for-each select="countries/country">  Country node:  <xsl:value-of select="current()"/><BR/>          </xsl:for-each>       </BODY></HTML>    </xsl:template> </xsl:stylesheet>   

Figure 3-12 shows the final result.

image from book
Figure 3-12: Parsing using the current() function is the same as .

How It Works

You created a new XSL style sheet and added HTML elements and a namespace. Then you added a for loop to scroll through all countries in the XML document. Next you pulled the text of each node within the for loop, and retrieve as the data is scrolled through. Last, you added a little more HTML text just to make it look nice.

image from book
 

XSL Method Syntax

Methods and their syntax are as follows:

  • absoluteChildNumber ("node-name") : Collection iteration number representing the sequence of a node within a set of siblings.

Siblings are all nodes within the same parent node.

A collection iteration number is the same as a subscript number in an array.

  • ancestorChildNumber ("parent-node-name") : Collection iteration number representing the sequence of the parent node ( ancestor ) with the parent nodes set of siblings.

  • childNumber ("parent-node-name") : Collection iteration number for the nodes of a parent node, relative to the parent nodes siblings, where all of those parent siblings have the same node name. In other words, where parent nodes have varying names , only the specified node name is included.

  • depth ("node-name") : XML document hierarchical tree depth number.

  • formatXXXX methods: formatDate (date), formatIndex (integer), formatNumber (number) and formatTime (time) all format specific data type values into more readable and user -friendly formats. For output.

  • uniqueID ("node-name") : An XML document is an object structure and therefore every node in an XML document has a unique identifier as a number. This function returns the unique identifier for a node within an XML document.

These methods are all inherited from the XML DOM and require the creation of XML DOM objects in order to execute. The XML DOM is already covered in Chapter 2 .

XSL Pattern Matching Syntax

So far in this chapter you have seen very simple pattern matching. Figure 3-13 shows various pattern matching options available when using XSL.

image from book
Figure 3-13: Parsing pattern matching options for XSL


Beginning XML Databases
Beginning XML Databases (Wrox Beginning Guides)
ISBN: 0471791202
EAN: 2147483647
Year: 2006
Pages: 183
Authors: Gavin Powell

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