Putting It All Together
It's not fair to take a peek at all these new toys if you can't play with them a bit. So here are some interesting examples that illustrate the new powers in XLST 2.0 and XPath 2.0. Listing 3
shows the source document used for the sample transformations. It's essentially the same as Listing 1
, but I've removed the schema to simplify adding an element to the file without worrying about validation.
Now, take a look at Listing 4
. I've employed a few new XSLT 2.0 operations, so I'll break down the code a bit. The highlighted XSLT 2.0 features appear in bold
The first interesting chunk of code concerns an old favorite of Perl programmers, regular expressions. I've decided to take Bert, the maker of all these fine products, out of the loop, so to speak, and hand the products over to a fellow named Tom. I do this not because I don't like Bert, but because I want to show how regular expressions have made their way into XSLT 2.0. The following chunk of source XML shows a <product>
The object is to transform the <name>
element in the input document...
...into the following HTML in the output:
Trying Out a Regular Expression
In XSLT 1.0 the only way to replace a string of text is to use a recursive template, such as the one found here
There's nothing wrong with using such templates, of course, but it would be nice to have a simple function for it. Now, with the help of regular expressions, there is. Rather than calling a complex template function, you can write simple code like this:
<xsl:value-of select="replace(., '(Bert)', 'Tom')"/>
The replace function takes three arguments. The first argument is the source string, the second is the part of the string that you want to replace, written as a regular expression, and the third is the replacement string or expression. The function replaces each instance of the target expression found in the source string with the replacement expression. I used simple literals for my regular expressions, but if you know regular expressions you can really do some serious string manipulations here.
for more information on how to use regular expressions.
There are some differences between the way you use regular expressions in Perl and the way you use them in XML Schema. To explore regular expressions specifically as they pertain to XML Schema see http://www.xfront.org/xml-schema/
Another thing I wanted to do was effortlessly output the value of each <quantity>
element within each <product>
element. Again, this kind of thing is pretty easily accomplished in XSLT 1.0 once you've learned the language, but now it's even easier because of one simple keyword named except. This keyword lets you restrict certain values from a specific sequence you are trying to generate. For example, to include all the following siblings of the name element, except the last one, you can write:
select="(following-sibling::*) except following-sibling::*"
The result outputs all the quantity
elements to HTML:
Note that in XSLT 1.0 you can accomplish the same thing with this select statement: