Chapter 4: Understanding Extensible Stylesheet Language for Transformation


 Download CD Content

Extensible Stylesheet Language for Transformation (XSLT) is a language using which you can transform an eXtended Markup Language (XML) document into any other format, such as HTML. XSLT stylesheets change the structure and type of an XML document

This chapter introduces XSLT, and explains its data model, expression, and templates. It also explains the variables and parameters used in XSLT.

Introducing XSLT

XSLT is an application of XML that specifies how data is sent over the Internet using XML. You can use the XML Path (XPath) language to retrieve elements and attributes of an XML document to perform transformation on XML data. During the transformation process, you can sort , add, and delete elements from an XML document using XSLT. You can also change the order of the elements in the output of an XML document using the expressions and templates in XSLT. The XML document that is transformed is called a source tree or the source document and the transformed document is called the result tree or result document.

The XSLT processor parses the source tree, by creating an XSLT tree from the stylesheet that contains the specification of the source tree. The XSLT processor uses the source tree to generate the result tree. Various elements in XSLT, such as stylesheet, value-of, for-each, sort, and text help to create data model of XSLT, which specifies the structure of the source tree.

Data Model

The data model of XSLT specifies the structure of the source tree, which consists of elements and attributes of an XML document. You can also refer to elements in an XML document as nodes. XSLT validates an XML document by creating a tree structure of elements and attributes present in the document. A tree of an XML document consists of various nodes, such as root, element, text, attribute, namespace, processing instruction, and comment.

The root node refers to the root element of the tree structure. An element of an XML document refers to the child node of the root element. The root node contains one or more child nodes but does not contain any text. The root node also contains processing instructions and comment nodes as its child nodes.

The data model of an XSLT consists of various elements, such as:

  • <xsl:stylesheet>

  • <xsl:value-of>

  • <xsl:for-each>

  • <xsl:sort>

  • <xs:text>

Each XSLT stylesheet starts with the stylesheet element. The syntax to declare the stylesheet element is:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

In the above syntax, xmlns is the attribute of the stylesheet element that provides a Uniform Resource Identifier (URI) of XSLT. You need to specify the URI, http://www.w3.org/1999/XSL/Transform, as a value of the xmlns attribute, to check that the elements and attributes are specified according to the specifications defined by W3C.

The value-of element displays the value of an element or attribute. The syntax to use the value-of element is:

 <xsl:value-of select="element_name/@attribute_name"/> 

In the above code:

  • The select attribute of the value-of element indicates the name of an element or attribute that displays its value on a Web browser.

  • The attribute name of an element must be prefixed with the @ sign.

  • The value-of element is an empty tag that includes the slash mark, /, before the right angle bracket of the tag.

Enter the following code in the stylesheet to use the value-of element:

 <xsl:value-of select="STUDENT"/> 

The above code displays the value of the STUDENT element of an XML document. Enter the following code to display the value of an attribute:

 <xsl:value-of select="@id"/> 

The above code displays the value of the id attribute.

The for-each element of XSLT processes data each time a specified pattern occurs in the XML document. This element lets you use looping construct in XSLT as in the C or C++ language. The syntax to use the for-each element is:

 <xsl:for-each select="pattern"> <!-- Data to be processed --> </xsl:for-each> 

In the above syntax, the select element can contain three patterns:

  • The element pattern: The XSLT processor processes the data for each instance of a given element.

  • The root/sub-element pattern: The XSLT processor processes the data each time a specified sub-element is encountered under the root element.

  • The ancestor//sub-element pattern: The XSLT processor processes the data each time a specified sub-element is encountered under the ancestor element.

Enter the code in Listing 4-1 to use the for-each element with the root/sub_element pattern:

Listing 4-1: Using the for-each Element
start example
 <xsl:for-each select="STUDENTDATA/STUDENT"> <font color="red" size=20><b><xsl:value-of select="NAME" /></b></font> <xsl:value-of select="@id"/> </xsl:for-each> 
end example
 

In the above code:

  • The root/sub_element pattern is used, which processes the data when the element encounters the STUDENT sub element of the STUDENTDATA root element.

  • The <font> element of the HTML document specifies the font size and color for the value of the NAME element.

  • The <b> element of the HTML document displays the value of the NAME element in bold. The value of the id attribute is displayed in normal text.

The sort element of XSLT arranges data in the ascending or descending order. The sort element has four attributes, select, order, case-order , and data-type. The syntax to use the sort-element is:

 <xsl:sort select="element_name/expression"  order="ascending/descending"  case-order="upper-first/lower-first"  data-type="text/number"/> 

In the above syntax:

  • The select attribute: Indicates the expression on which sorting is performed.

  • The order attribute : Indicates the order of sorting.

  • The case-order attribute : Indicates the order of sorting for the uppercase and lowercase characters .

  • The data-type attribute : Specifies the data type of the expression that is to be sorted.

Listing 4-2 shows the code to use the sort element:

Listing 4-2: Using the sort Element
start example
 <xsl:for-each select="STUDENT"> <xsl:sort select="AGE" data-type="number" order="ascending"/> <xsl:value-of select="NAME"/> <xsl:value-of select="AGE"/> </xsl:for-each> 
end example
 

The above code shows that the for-each element traverses every sub element of the STUDENT element. The code displays the value of the NAME and AGE elements in the ascending order of the age element.

The default value for the order attribute is ascending, and the default value for the data-type is text.

Templates

XSLT contains a collection of template rules that provide specifications to convert an XML document and display the results on a Web browser. A template rule has two parts , which contain information about the transformation process. The first section specifies the part of an XML document to be converted, and the second specifies the format of the output.

The XSLT processor accepts the XML document and templates, processes the data, and generates an output.

The template and apply-templates elements define the template rules in XSLT. The syntax to use the template element is shown in Listing 4-3:

Listing 4-3: Using the syntax Element
start example
 <xsl:template match="expression"> <!--Data to be processed--> </xsl:template> 
end example
 

In the above syntax, the value of the match attribute indicates an element of an XML document that checks against the template rules.

Various expressions that you can use in the match attribute are:

  • / : Indicates the root element that matches all the elements, such as processing instruction, comments, and the root element of the XML document. It is also known as document root.

  • element : Indicates an element of an XML document, which is processed when the XSLT processor encounters it.

  • * : Indicates a wildcard character that matches any element in an XML document.

  • element1 element2 : Indicates two elements, which are processed when the element1 and element2 are encountered in an XML document.

  • element [@attribute]: Processes data when an attribute of an element is encountered.

  • element [@attribute= value ] : Matches all the elements that contain the specified value of the attribute defined in the @attribute attribute.

  • root_element/sub_element : Processes data when the sub elements of the root element are encountered.

  • ancestor//sub_element : Processes data when the sub element under the ancestor element is encountered.

The apply-template element enables the XSLT processor to apply the templates, specified by the template element, to the selected XML elements. The syntax to use the apply-template is:

 <xsl:apply-template [select="expression"]> 

The above code shows that the template rules apply on the expression specified in the select attribute of the apply-template element. In the syntax, the select attribute is optional.

The default value of the apply-template element is node(), which specifies that the template matches the sub elements of the current node. The expression specified in the select attribute of the apply-template element matches the expression specified in the match attribute of the template element. Each node contains a base URI that resolves the value of the attributes. The URI of the document entity refers to the base URI of the document root node.

Listing 4-4 shows how to use the template and apply-template elements:

Listing 4-4: Implementing Template Rule
start example
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> 
end example
 

In the above listing:

  • The <xsl:template match="/"> element indicates the template rule.

  • The value of the match attribute specifies the document root of an XML document.

  • The apply-template element indicates that the template affects all the elements of an XML document.

  • The listing does not contain a format style, so the output displays the result with the default font size, face, and color.

You can define your own template rules using the template and apply-templates elements, as shown in Listing 4-5:

Listing 4-5: Creating Template Rules
start example
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="STUDENTDATA/STUDENT"/> </xsl:template> <xsl:templa1te match="STUDENTDATA/STUDENT"> <font color="red"> <LI><xsl:apply-templates/></LI> </font> </xsl:template> </xsl:stylesheet> 
end example
 

The above listing shows that the template, STUDENTDATA/STUDENT, is created for the document root. The XSLT processor searches for the STUDENTDATA/STUDENT template. It then sets the font color of the value of all the child elements of the STUDENT element to red, and displays them in bulleted list.

Expression

In addition to the templates, XSLT also contains expressions, which correspond to the nodes of an XML document being transformed. The main functions of the XSLT expressions are:

  • Identify the node to be transformed.

  • Specify the condition to process the node.

  • Insert character data in the result tree.

The XPath expression defines the format of the expression for XSLT. It retrieves data based on specified conditions, and performs calculations on numeric data. The XPath expression can contain various characters, which are:

  • \n : Indicates a newline character.

  • . : Indicates a single character.

  • \d : Indicates a numeric character.

  • \s : Indicates the whitespace characters, such as horizontal tab, line feed, and carriage return.

  • [a-d] : Indicates the characters from a to d.

  • * : Indicates zero or more occurrences of the whitespace characters.

  • + : Indicates one or more occurrences of the whitespace characters.

  • ? : Indicates zero or one occurrence of the whitespace characters.

  • \? : Searches for the question mark character, and overwrites the special meaning of the question mark expression.

You can combine the characters in the XPath expression to denote specific meaning. For example, the expression character, \s*, used in the XPath expression, specifies zero or more occurrences of the whitespace characters. The expression character, \s+, specifies at least one or more occurrences of the whitespace characters.

An expression in XSLT uses the following functions:

  • tokenize(string, delimiter ) : Returns the number of strings that are separated by the delimiter.

  • matches(string1, string2) : Returns the value, true, which specifies that the value of the first parameter is same as the value of the second parameter.

  • replace(string1, string2, string3) : Searches the second string within the first string and replaces the searched string with the third string specified in the function.

You can specify expressions within a function to change the format of an XML document.

Listing 4-6 shows the use of the expression characters and functions:

Listing 4-6: Using the XSLT Expressions and Functions
start example
 <xsl:template match="price"> <xsl:match> <xsl:attribute name="pattern"> <xsl:value-of select=matches(.,".*$\d\.\d.*") /> </xsl:attribute> <xsl:value-of select=replace(., "$\d\.\d", "$#.#") /> </xsl:match> </xsl:template> 
end example
 

The above listing shows that the template changes the price element by adding the pattern attribute. The pattern attribute stores the value, true or false, depending on the result of the match() function. The content of the price element changes according to the result of the replace() function. In the above code:

  • The first parameter of the matches() function indicates the entire text of the document.

  • The second parameter of the matches() function indicates the expression that specifies any text before and after the dollar sign, followed by a digit, period, and another digit.

  • The second parameter of the replace() function indicates the expression that specifies the dollar sign followed by a digit, period, and another digit.

  • The XSLT processor replaces the text of the pattern element with the third parameter, $#.#, of the replace() function.

Listing 4-7 implements the expressions and functions of XSLT on the following XML document:

Listing 4-7: Implementing the Expressions and Functions on XML Document
start example
 <product> <price>Price of Pen: .8</price> <price>Price of Pencil: </price> </product> 
end example
 

In the above code:

  • The first instance of the price element matches the expression. As a result, the pattern attribute of the price element stores the value, true.

  • The second instance of the price element does not match the expression, because it does not contain a period followed by a digit.

The template rule specified in Listing 4-3 changes the XML document, as shown in the following code:

 <?xml version="1.0" encoding="UTF-8"?> <product> <price pattern="true"> Price of Pen: $#.#</price> <price pattern="false">Price of Pencil: </price> 

</product>

The above code shows that the pattern attribute of the second instance of the price element contains the false value because it does not match the expression. As a result, the text, $1, of the price element is not replaced with the text, $#.#.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

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