Mastering Resin

XML, XPath, XSL, and StyleScript

All of our examples thus far are based on several technologies that you must understand before moving any further. These technologies include XML, XPath, XSL, and StyleScript. In this section, we discuss each of these technologies and explain how they work along with Resin to provide dynamic content to site users.

XML

You write XTP pages using HTML and other user-defined tags. For the most part, the XTP page can be fundamentally broken down into a format that resembles XML. By this we mean there are tags arranged in a pseudo-hierarchy, with attributes and enclosed values for some of the tags.

When the Resin server is presented with an XTP page, its first task is to parse the XML. Parsing the XML allows the system to "look" at each of the tags and determine if any action should take place. The parsing can be accomplished in two ways. The first is called the Document Object Model (DOM). In the DOM parsing methodology, a DOM tree is created that represents the entire XTP page. An alternative parsing method, called SAX, doesn't create an internal data structure but instead calls defined methods when various pieces of the XML are recognized. For instance, a method is called when a beginning tag is recognized.

Once the XTP page has been parsed using DOM, you can use a technology called XPath to match the nodes in the tree.

XPath

XPath is a specification-based language created for the express purpose of finding nodes in a DOM tree. As a defined language, XPath can be used independently of other technologies like XSL. The primary semantics defined in XPath revolve around matching patterns. The following is an explanation of the core patterns. Eventually, the patterns are used in either XSL or Style-Script to provide functionally to the system. As we examine the XPath pattern-matching syntax, consider the following example XML file:

<HTML> <BODY> <table border='1'> <doTableHeader/> <handleRow/> </table> <table> <doTable2Header/> <handleRow value='0'/> <footer>2</footer> </table> </BODY> </HTML>

Although we haven't looked at XSL much yet, let's use the <xsl:template match='<pattern>' /> tag for our examples. This tag defines an action that needs to take place when the pattern found in the match attribute is successful. As mentioned, the following list provides an overview of the XPath pattern-matching syntax available to the developer when writing XSL.

Expressions

In most of the pattern-matching strings, you can use expressions to further clarify and narrow the selection of nodes in the document. Table 5.1 lists operators are available for formulating expressions.

Table 5.1: Available Operators

OPERATOR

DESCRIPTION

+

Addition

-

Subtraction

*

Multiplication

div

Division

mod

Modulus (division remainder)

=

Like (equal)

!=

Not like (not equal)

<

Less than

<=

Less or equal

>

Greater than

>=

Greater or equal

or

or

and

and

For example, we can match based on the value of an attribute:

//table[@border > '5']

This code matches when a <table> tag is found that has a border attribute greater than 5.

XPath Functions

In several of our examples, we use XPath functions such as last() and position() to give a level of dynamic selection to the processing. Table 5.2 lists the available functions.

Table 5.2: XPath Functions

FUNCTION NAME

PURPOSE

boolean(<object>)

Returns a Boolean value from converting <object>.

ceiling(<number>)

Returns the closest integer greater than <number>.

concat(<string1, string2, ...>)

Returns a new string by concatenating the supplied strings.

contains(<srcstring, substring>)

Returns True if the substring is found within the srcstring.

count(<node-set>)

Returns a total count of all nodes matching the supplied node-set pattern.

false()

Returns a value of False.

loor(<number>)

Returns the closest integer less than <number>.

id(<ref>)

Returns the node having id attribute equal to <ref>.

if(<condition>, <a>, <b>)

If <condition> is true, returns <a>, else returns <b>.

lang(<language>)

Returns True if xml:lang of context node is equal to <language>.

last()

Returns the total count of nodes matching the current pattern.

local-part(<node>)

Returns the local part of <node>'s name.

name(<node>)

Returns <node>'s name.

namespace(<node>)

Returns <node>'s namespace URL.

normalize-space(<string>)

Returns a new string with normalized whitespace in <string>.

number(<object>)

Returns a Java double based on converted <object>.

round(<number>)

Returns <number> rounded to the nearest integer.

starts-with(<srcstring>, <headstring>)

Returns True if <srcstring> starts with <headstring>.

string(<object>)

Returns a new string based on <object>.

string-length(<string>)

Returns the length of <string>.

substring-after(<srcstring>, <substring>)

Returns a new string using the substring of <srcstring> after the matching <substring>.

substring-before(<srcstring>, <substring>)

Returns a new string using the substring of <srcstring> before the matching <substring>.

sum(<node-set>)

Returns a number based on the sum of all nodes designated by <node-set>.

translate(<srcstring>, <from>, <to>)

Returns a new string after converting characters using Unix 'tr' functionality.

true()

Returns True.

Many of the functions are used in XSL tags like <xsl:value-of> as well as in <xsl:template>. For example:

<xsl:template match="/table[if(@border>'5', bigTag, smallTag)]" > <xsl:value-of select="count(@border)" /> </xsl:template>

XSL

Now that you know all about matching tags in your XTP pages, let's look at the tags available in XSL that allow you to do something useful with the matches found. XSL consists of various tags starting with the prefix <xsl:. The following list of tags provides you with a steppingstone to fully utilizing the power found in XSL.

In this example, we import a style sheet called standard.xsl. In standard.xsl, there is a template with a match='account'. The style sheet defined here also has a template with a match on account. By using the <xsl:apply-imports/> tag, we can output the <font> HTML, then use the template defined in standard.xsl and finally close the </font> tag.

In this example, we match on the account node and then attempt to match any child node with the name of 'name'.

This example shows how to add a new attribute to the account tag.

In this example, we create an attribute set that includes the name of the set and individual <xsl:attribute> tags.

This code uses the <xsl:choose> tag to display an account's number and name in a table. If the account number is greater than 99910, then the name is highlighted.

This example uses the <xsl:copy> tag to copy all child nodes of the city node to the output without any processing.

In this example, we define an XSL variable and use <xsl:copy-of> to output the variable's contents as needed.

This example looks through all of the nodes in the document with the name account. For each of those nodes, a new node is created in the DOM called accountnumber, with a value associated with the number node.

The <xsl:for-each> tag is very useful in that it allows the style sheet to loop through all of the nodes based on a specific pattern match.

Here the <xsl:if> tag is used to determine whether or not specific values are sent to the output.

In this example, we import an XSL style sheet called newXSL.xsl into the current one.

In this example, we created a variable called header. We can use the variable throughout the XSL style sheet to display header information by using the <xsl:param> tag.

In this example, the processing instruction xml-stylesheet is added to the DOM.

Here we find all of the accounts, sort them by number node value, and display the name for each account.

In this example, values are displayed for the name and state nodes defined as children under the accounts node.

In this example, we created a variable called header. We can use the variable throughout the XSL style sheet to display header information by using the <xsl:param> tag.

Категории