Basic XSL Elements Syntax

XSL can be used to manipulate, sort , and filter XML data. Transformations can include well-formed HTML. Well-formed means that any HTML tag can be used, subject to the stricter syntax rules of XML. All start tags are paired with end tags and are nested correctly. Well- formed HTML can be displayed by the browser, or further manipulated by XML tools.

XSL enables you to define templates for your output into which data from the XML source is delivered. Each template defines a pattern that identifies elements in the source tree and defines the resulting output subtree to be generated. The XSL transformation processor merges data from the XML source document with the template. By combining a set of template fragments into a style sheet, XSL can be used to perform transformations driven by XML document data. These transformations are useful for making XML data and XML documents much more presentable and refinable.

A very small set of XML elemental functionality can be used to define all the formatting and filtering functionality of XSL, as shown in Figure 3-1.

image from book
Figure 3-1: The XSL elements

The order and division of XSL elements shown in Figure 3-1 are sorted to allow for easiest explanation. In some cases elements not yet explained have to be included in order to facilitate explaining a particular element.

The fundamental structures of XSL syntax consist of the elements that make up XSL style sheets, as shown in Figure 3-1. Lets go through these elements one by one, as they are displayed in Figure 3-1.

Processing Instruction Elements

A node processing instruction includes a processing instruction, such as an <xsl: . . . > tag in the output. As shown in Figure 3-1, the node processing instruction elements are xsl:processing-instruction , xsl:stylesheet , xsl:comment , and xsl:script .

xsl:processing-instruction

The xsl:processing-instruction element can be used to place a processing instruction into an XSL script as follows :

   <xsl:processing-instruction name="xml-stylesheet" xmlns=" http://www.w3.org/TR/WD- xsl"> </xsl:processing-instruction>   

The preceding command produces the following output:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">   

In fact, it is easier simply to hard code the specific processing instruction element, such as the xsl:stylesheet element.

The xsl:processing-instruction element has also been called the xsl:pi element in the past.

xsl:stylesheet

The root element of an XSL style sheet is the xsl:stylesheet or the xsl:transform elements.

The xsl:transform element is ignored for the purposes of simplicity in this book as it is the same semantically as xsl:stylesheet .

XSL style sheets containing HTML code do not require the xsl:stylesheet element.

The basic syntax for the xsl:stylesheet element is as follows:

   <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/TR/WD-xsl" language="  language  ">   

Attributes for the xsl:stylesheet element are as follows:

  • version : Typically this can be included in the XML document that the XSL style sheet is to be applied to, but it can be included in the XSL style sheets xsl:stylesheet element as well.

  • xmlns : This attribute determines the default namespace, generally pointing to the W3C definition.

  • language : Apply a scripting language as a default for an entire XSL style sheet.

Other more obscure attributes are default-space and indent-result , both preserving white space. There is also a language attribute that can define a scripting language as applied to the entire XSL style sheet. Finally there is a result-ns attribute that defines if the output is to be parsed as HTML, XML, or otherwise . These additional parameters are a little beyond the scope of this book.

Remember this for now: the xsl:stylesheet element can contain zero or more xsl:template elements. The root template contains the / pattern in the xsl:template match attribute. When applying templates to an XML document, each template is applied as an individual container, within the root template.

xsl:comment

The comment element causes the parser to ignore text enclosed within the xsl:comment element like this:

   <xsl:comment> Place comments into an XSL script like this </xsl:comment>   

xsl:script

Various XSL script elements can place scripting language code blocks into XSL style sheets. Where the xsl:stylesheet element language attribute applies a specific scripting language to an entire XSL file, the xsl:script element allows insertion of a language-specific script into an XSL style sheet. The syntax is as follows:

   <xsl:script language="JavaScript"> for (n in collection) {    document.writeln ("Subscript: " + n);    document.writeln ("Value: " + collection[n]); } </xsl:script>   

Transformation Elements

Transformation elements allow for transformations of XML data, formatting one or more parts of an XML document into a variable output display. As shown in Figure 3-1, the transformation elements are xsl:template , xsl:apply-templates , and xsl:call-template .

xsl:template

The xsl:template is used to define a template that can be applied to nodes of a particular type and context. This means that one can pattern match a specific tag within an XML file, and then designate that a tag and its contents are displayed in a manner specified by the template. This also implies that differing tags accessed within the same XSL style sheet can even have different templates applied in the same style sheet. The result is multiple possible display formats for the same XML tree. This means that the same data (in the same XML document) can be made to appear differently on the web, depending on what template is applied. The syntax is as follows:

   <xsl:template language="  language  " match="  context  ">   

Attributes for the xsl:template element are as follows:

  • language : A template can use a different scripting language. Thus, one can change the scripting language within a specific template. As you already know, a template can apply a specific transformation to a specific part of an XML document. This attribute will be ignored at this stage as being too fiddly and complicated. I thought it was interesting to mention it.

  • match : This attribute allows pattern matching in an XML document. Pattern matching allows the template to select those certain parts of an XML document as already mentioned.

What is really interesting about the xsl:template tag is that not only can a consistent format be applied across an entire XML document, but also you can even find separate parts of an XML document within each template, and then apply those different templates to the XML document as it is parsed.

Figure 3-2 shows an XML document containing comments about cities, within states, within countries .

image from book
Figure 3-2: An XML document containing commentary , within cities, within states, within countries

The xsl:template element can be used to match and retrieve specific nodes. In the XSL style sheet that follows, the template is set to /, representing the root node of the XML document. In the case of Figure 3-2, the root node is the <world> node. The xsl:value element is used to select all text values within the subtree referenced as world/countries (everything in the XML document):

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">    <xsl:template match="/">       <xsl:value-of select="world/country"/>    </xsl:template> </xsl:stylesheet>   

The <xsl:value-of> element extracts text values from a node and is covered in detail in the next section.

The result of the previous script is as shown in Figure 3-3, where the xsl:template is digging down into the first <country> node, within the root node ( <world> ). It finds all the text values for <name> and <type> elements within the country of Canada. This is not too useful.

image from book
Figure 3-3: A single xsl:template finds all text values within the first matched node

The next example, as shown in the script that follows, finds all text items in the entire XML document. The result is shown in Figure 3-4.

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">    <xsl:template match="/">       <xsl:value-of select="*"/>    </xsl:template> </xsl:stylesheet>   

The example shown in Figure 3-4 finds all text items, both <name> and <type> elements, for both Canada and the United States.

image from book
Figure 3-4: A single xsl:template finds all element text values within the entire XML document

xsl:apply-templates

The xsl:apply-templates element applies a template to all or part of an XML document sometimes based on a pattern match. This is the syntax for the xsl:apply-templates element:

   <xsl:apply-templates order-by="  sort-criteria-list  " select="  pattern  ">   

Attributes for the xsl:apply-templates element are as follows:

  • order-by : A semicolon separated list. A plus ( + ) sign denotes ascending order and a minus ( ) sign denotes descending order.

  • select : The pattern determines which nodes have a specific template applied to them.

At this stage the xsl:template element does not seem too useful. Combining the xsl:template element with the xsl:apply-templates element gets interesting. Remember that during the description of the xsl:stylesheet element earlier, you were told that multiple templates can be applied to individual parts of an XML document. This allows for multiple display formats across the same XML document.

The xsl:stylesheet element can have multiple xsl:template elements. The root template, indi cated by the pattern, is the first template processed .

The following script contains a few XSL syntax elements that will be introduced later on in this chapter:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/">    <HTML><BODY>    <xsl:for-each select="world/country/state/city">       <xsl:apply-templates select="name"/>       <xsl:apply-templates select="type"/>    </xsl:for-each>    </BODY></HTML> </xsl:template> <xsl:template match="name">    <B><U><xsl:value-of select="name"/><xsl:apply-templates/></U></B> </xsl:template> <xsl:template match="type">    <I><xsl:value-of select="type"/><xsl:apply-templates/></I> </xsl:template> <xsl:template match="text()"><xsl:value-of/></xsl:template> </xsl:stylesheet>   

Unlike in previous examples where the xsl:value-of element was used to retrieve a single node, the xsl:for-each element applies a looping construct to the entire XML document. The for-each loop scans the entire XML document for all matching city names and types.

The text() method on the second to last line of the script retrieves the node text values within the <name> and <type> nodes.

HTML tags are added to the preceding XSL script, turning the application of XSL transformations to XML documents into an HTML document. Additionally, HTML tags can be added allowing for simplistic display formatting features such as colors, underline, bold, and italic fonts: essentially , anything that HTML can do.

The point of the preceding script is shown in Figure 3-5, where two templates are defined within the same XML document, and different formatting is applied within each template.

image from book
Figure 3-5: Applying multiple xsl:template elements using the xsl:apply-templates elements

Figure 3-6 shows the example script in Figure 3-5 executed in a browser. The names of cities appear with underline and bold. The <type> nodes containing commentary on a city appear as italicized text.

image from book
Figure 3-6: xsl:template and xsl:apply-template elements apply varying formats

xsl:call-template

The xsl:template allows formatting from XSL onto XML documents. The xsl:apply-templates element allows application of multiple templates into a single XML document. The xsl:call-template goes one small step further, allowing explicit naming of templates and access to templates. It also allows the ability to pass parameters into those named templates from outside of the XSL style sheet coding. Using the xsl:call-template without a parameter looks like this:

   <xsl:template name="  template name  ">    ... template contents ... </xsl:template>   

And the template is executed like this:

   <xsl:call-template name="  template name  ">   

Similarly, with a parameter it looks something like this:

   <xsl:template name="  template name  ">    <xsl:with-param name="  parameter name  "/>    ... template contents ... </xsl:template> <xsl:call-template name="  template name  " parameter="  parameter name  ">   

Node Creation Elements

Node creation elements allow generation of new nodes into XML documents when executed including an XSL style sheet transformation. As shown in Figure 3-1, the node creation elements are xsl:element , xsl:attribute , xsl:copy , and xsl:copy-of .

xsl:element

The xsl:element element creates a named element.

A named element can be accessed directly by the name of that element.

The syntax is as follows:

   <xsl:element name="  element  ">   

In the above example, name is the name of the element to be created.

The < . . . > angle brackets are part of XSL syntax and thus the syntax line <xsl:element name="element"> is not Backus-Naur syntax notation.

The following sample XSL code snippet creates an HTML form <INPUT> tag field (for data entry). This code snippet will ultimately be part of a web page using XML and XSL formatting, where the XSL code includes HTML tags, which you will see shortly:

   <xsl:element name="INPUT"> ... </xsl:element>   

The HTML equivalent of the preceding sample script snippet is something like this:

   <HTML><BODY> ... <INPUT ... > ... </BODY></HTML>   

There are no attributes in the preceding example yet, so lets examine attributes next.

xsl:attribute

The xsl:attribute element adds attributes to elements. The syntax is as follows:

   <xsl:attribute name="  attribute  ">   

The previous sample, in the previous section xsl:element, is enhanced using the xsl:attribute element. This sample code snippet adds attributes to the HTML <INPUT> tag. Again, this code snippet will ultimately become part of a web page using XML and XSL formatting, where the XSL code includes HTML tags:

   <xsl:element name="INPUT">    <xsl:attribute name="VALUE"></xsl:attribute>    <xsl:attribute name="SIZE">40</xsl:attribute> </xsl:element>   

Once again, the HTML equivalent of the preceding sample script snippet is something like this:

   <HTML><BODY> ... <INPUT VALUE="" SIZE="40"> ... </BODY></HTML>   

So, our example as shown previously now has both an HTML form <INPUT> tag (an input field), plus two attributes. The first attribute is called VALUE and the second attribute is called SIZE . HTML form <INPUT> tags can have both value and size attributes. The VALUE attribute determines a default value for an input field. The SIZE attribute determines the width of the field on the screen.

xsl:copy

The xsl:copy element duplicates a node to a new node having the same name, namespace, and type as the duplicated node. Attributes and child nodes of the source node will not be copied automatically. The syntax is as follows:

   <xsl:copy>   

The highlighted section of code shows that all text values, in all elements in an XML document, are included when the new node is being created. The xsl:copy element simply duplicates all the textual contents of an XML document:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">    <xsl:template>       <xsl:copy>          <xsl:apply-templates  select="@*"/>  <xsl:apply-templates/>       </xsl:copy>    </xsl:template> </xsl:stylesheet>   

The XML document used in this example is shown in Figure 3-2, and contains comments about cities, within states, within countries. Figure 3-7 shows the result of placing the preceding example template formatting style sheet, shown in Figure 3-2, into the XML document.

image from book
Figure 3-7: An XML document containing continental and country populations

Figure 3-7 is identical to Figure 3-4 except Figure 3-4 uses the xsl:value-of element, as opposed to the xsl:copy element.

The example in Figure 3-7 is not really doing very much but simply selecting all text values within the XML document and copying all those values to the browser.

xls:copy-of

The xls:copy-of element duplicates nodes including subtree nodes and all descendant nodes and attributes. Basic syntax performs a pattern match:

   <xsl:copy-of select="  pattern  ">   

Following is an example application of the xls:copy-of element using the same XML document shown in Figure 3-2 again:

   <xsl:template match="world/country/state/city">     <xsl:copy-of select="name"/>     <xsl:text> </xsl:text>     <xsl:copy-of select="type"/> </xsl:template>   

Data Retrieval Elements

A data retrieval element is used to extract data from an XML document and also push data to the output. As shown in Figure 3-1, the node data retrieval elements are xsl:value-of , xsl:output , and xsl:text .

xsl:value-of

The xsl:value-of allows the extraction of text values from a node. The syntax is as follows:

   <xsl:value-of select="  pattern  ">   

Here, pattern is a pattern matching a node. Specific details of all patterns are covered later in this chapter. Any specific patterns used before that section will be very briefly described. The default value for pattern is .. This finds the text value of the current node.

In addition, the now fully functional XSL script shown in the following code will create an HTML form <INPUT> tag field (for data entry). Also, this time all the text values in the XML document will be set into the input field as a default value, as shown in Figure 3-8.

   <?xml version="1.0"?> <HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl"><BODY>    <xsl:element name="INPUT">       <xsl:attribute name="VALUE">          <xsl:value-of select="*"/>       </xsl:attribute>       <xsl:attribute name="SIZE">40</xsl:attribute>    </xsl:element> </BODY></HTML>   
image from book
Figure 3-8: An XML document containing an HTML form input field

The HTML equivalent of the preceding sample script snippet would be something like this:

   <HTML><BODY> ... <INPUT VALUE="Canada Quebec Montreal French British Columbia Vancouver Nice United States New York New York City Commercial Buffalo Big Snow Balls California San Francisco Silicon Valley Los Angeles Polluted San Diego Sunny" SIZE="40"> ... </BODY></HTML>   

xsl:output

The xls:output element allows production of output in a specific format, such as XML, HTML, or even text. The syntax is as follows:

   <xsl:output select="  method  ">   

In the preceding line of code, method is set to values such as xml , html , or text .

xsl:text

The xsl:text element quite literally places raw text into the output. The syntax is as follows:

   <xsl:text> this is some text </xsl:text>   

Control Structure Elements

Control structure elements allow for a certain amount of programmatic decision-making control for XSL style sheet, as well as XML document transformations and formatting. As shown in Figure 3-1, the control structure elements are xsl:if , xsl:for-each , xsl:choose (including xsl:when and xsl:otherwise ), xsl:sort , xsl:variable , and xsl:parameter .

xsl:if

The xsl:if element allows optional selection of execution paths. The syntax is as follows:

   <xsl:if test="  conditional expression  ">   

If conditional expression evaluates to true, whatever is contained between the <xls:if> and </xls:if> tags will be executed. In the following example, a line is drawn using the HTML <HR> tag whenever there is a change in the name of the continent in the XML document:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">    <xsl:template>       <xsl:apply-templates select="@*"/>          <xsl:if test="@name[.='Africa']"><HR/></xsl:if>          <xsl:if test="@name[.='Asia']"><HR/></xsl:if>          <xsl:if test="@name[.='Europe']"><HR/></xsl:if>          <xsl:if test="@name[.='Latin America and the Caribbean']"><HR/></xsl:if>          <xsl:if test="@name[.='North America']"><HR/></xsl:if>          <xsl:if test="@name[.='Oceania']"><HR/></xsl:if>          <xsl:value-of select="@name"/>       <xsl:apply-templates/>    </xsl:template> </xsl:stylesheet>   

The preceding script is the same example used in Figure 1-7 from Chapter 1.

xsl:for-each

The xsl:for-each element can be used to apply a template, looping repeatedly through an XML document. In terms of XML structure, it is a little like a collection iterator, applying the same transformation and formatting to repetitive groups. A repetitive group would be each item in the collection. The syntax is as follows:

   <xsl:for-each select="  pattern  " order-by="  sort-criteria-list  ">   
  • select : A pattern used to find all the nodes to be looped through.

  • order-by : A semicolon-separated list of nodes names to sort by. An ascending sort is the default and indicated by a plus sign (optional +). A descending sort is indicated by a minus sign ( ˆ ).

The order-by parameter is obsolete in favor of the xsl:sort element.

The example that follows shows iteration through a loop, contained within a loop, within a loop. The outer for-each loop iterates through countries, the next inner one through states within countries, and the innermost loop within cities within states. Note how each successive loop does not have to parse for what its parent loop parses for. Each loop provides each enclosed loop with its iterative object. In other words, when you parse for cities you parse for cities/city within each states/state , and not world/countries/country/ states/state/cities/city :

   <?xml version="1.0"?> <HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background- color:#EEEEEE">  <UL><xsl:for-each select="world/countries/country">  <LI><FONT COLOR="red"><xsl:value-of select="name"/></FONT></LI>  <UL><xsl:for-each select="states/state">  <LI><FONT COLOR="green"><xsl:value-of select="name"/></FONT></LI>  <UL><xsl:for-each select="cities/city">  <LI><FONT COLOR="blue"><xsl:value-of select="name"/></FONT>             is <xsl:value-of select="type"/></LI>       </xsl:for-each></UL>    </xsl:for-each></UL> </xsl:for-each></UL> </BODY></HTML>   

Figure 3-9 shows the result of the XSL style sheet applied to the XML document. The XML document used in Figure 3-9 is the same example used earlier in this chapter, in Figure 3-2.

image from book
Figure 3-9: An XML document using the xls:if element

xsl:choose, xsl:when and xsl:otherwise

The xsl:choose , xsl:when and xsl:otherwise elements constitute a case statement. The xsl:choose element creates the code block. One or more xsl:when elements allow for explicit pattern matches. xsl:otherwise executes a block of code if all xsl:when conditional expressions have failed. There must be at least one xsl:when element and the xsl:otherwise element is optional. The syntax is as follows:

   <xsl:choose>    <xsl:when test="conditional expression">       ...    </xsl:when>    <xsl:when test="conditional expression">       ...    </xsl:when>    <xsl:otherwise>       ...    </xsl:otherwise> </xsl:choose>   

The following XSL style sheet breaks up the populations by the sizes of those populations, and highlights those that are excessive:

   <HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background- color:#EEEEEE"> <H2>Populations</H2><TABLE> <xsl:for-each order-by="@name" select="populationInThousands/world/continents/ continent/countries/country">    <TR>       <xsl:choose>          <xsl:when test="year1998[. $gt$ 1000000]">             <TD><xsl:value-of select="@name"/> is <SPAN STYLE="font-weight:bold; color:red"><xsl:value-of select="year1998"/></SPAN></TD>          </xsl:when>          <xsl:when test="year1998[. $gt$ 100000]">             <TD><xsl:value-of select="@name"/> is <SPAN STYLE="font-weight:bold; color:green"><xsl:value-of select="year1998"/></SPAN></TD>          </xsl:when>          <xsl:otherwise>             <TD><xsl:value-of select="@name"/> is <xsl:value-of select="year1998"/></TD>          </xsl:otherwise>       </xsl:choose>    </TR> </xsl:for-each> </TABLE></BODY></HTML>   

Figure 3-10 shows the result of running the preceding XSL style sheet script.

image from book
Figure 3-10: An XML document using the xls:choose, xls:when, and xls:otherwise elements 73

Figure 3-11 shows a portion of the XML document used for the formatting and transformation shown in Figure 3-10.

image from book
Figure 3-11: Countries and their populations

xsl:sort

The xls:sort element is used to sort the output, with either an xls:for-each or xls:apply-templates elements. There are numerous parameters, of which only the most significant are shown in the following syntax:

   <xsl:sort select="  pattern  " order="ascending  descending">   

The following example XSL style sheet uses the xsl:for-each element, and an order-by attribute to impose sorting on XML document nodes:

   <xsl:for-each order-by="@name" select="root/ ...">  ...  </xsl:for-each>   

The xsl:sort elements can be used to replace the order-by attribute, as shown in the following:

   <HTML xmlns:xsl="  http://www.w3.org/1999/XSL/Transform  "> <BODY> <TABLE> <xsl:for-each select ="root/ ...">    ...    <xsl:sort select="@name"/> </xsl:for-each> ... </TABLE></BODY></HTML>   

The namespace for the HTML tag xmlns (XML namespace) attribute is changed in the preceding script. This applies to the most recent version of the XSL or XSLT definition from the W3C.

xsl:variable

The xsl:variable element can be used to declare variables in XSL style sheet scripts. Those variables can be global to the entire style sheet or local to a particular section of that XSL style sheet (within an xsl:template element. The syntax is as follows:

   <xsl:variable name="  variable-name  " select="  pattern  ">   

As you can see from the preceding syntax, a variable can be both declared and set using a pattern match selection from the attached XML document.

A variable is then accessed in the XSL style sheet using a dollar sign ($) reference character as in the following example code snippet:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:variable name="foo">funky-foo</xsl:variable>    <xsl:template match="/">  <xsl:copy-of select="$foo"/>  </xsl:template> </xsl:stylesheet>   

The only problem with this element is that an xsl:variable element does not create a true program ming variable because xsl:variable elements can be set only once within the script.

xsl:param

The xsl:param element is used to declare parameters in XSL style sheet scripts. As for the xsl:variable element, a parameter is declared locally within an xsl:template element, or globally. The syntax is as follows:

   <xsl:param name="  parameter-name  " select="  pattern  ">   

The xls:param element can be both declared and set using a pattern match selection from the attached XML document.

A parameter is accessed in the XSL style sheet using a dollar sign ($) reference character as in the following example code snippet:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:param name="foo">funky-foo</xsl:param>    ...    <xsl:template match="/">       <xsl:value-of select="$foo"/>    </xsl:template> </xsl:stylesheet>   


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