1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello XML Technologies
2 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Extension Elements The XSL processor understands how to process xsl:template, xsl:apply-templates, xsl:if, xsl:for-each, etc –That is, it understands the vocabulary in the XSL namespace XSL Processor implementers oftentimes provide additional elements that you may use in your stylesheet –These extension elements will belong to a namespace defined by the implementer
3 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Example Extension Element: instruct the xsl processor to output to another file Many of the xsl processor implementers provide an extension element that instructs the xsl processor to output the contents of the element to another file. –Thus, your stylesheet can generate multiple output files! XSL Processor XML XSL
4 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Vendor-specific Each implementor gives the extension element a different name: –saxon calls it: output –xalan calls it: write
5 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. How to use an extension element 1. Declare the namespace that the extension element belongs to: saxon: xmlns:saxon=" xalan: xmlns:xalan=" 2. Indicate that any element that is namespace qualified by the prefix is an extension element, i.e., it has a specific meaning and should be processed using the implementer's code: saxon: extension-element-prefixes="saxon" xalan: extension-element-prefixes="xalan" 3. Use the extension element: saxon: -- anything in here will go to the file specified --- xalan: -- anything in here will go to the file specified ---
6 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Problem Write a stylesheet which outputs the platinum members in one file, the gold members in another file, and the third file is an index to the other two files.
7 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. FitnessCenter.xsl FitnessCenter.xml XSL Processor gold.xml new-FitnessCenter.xml platinum.xml
8 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. This element instructs an xsl processor to copy to the output file the element selected by xpath, plus all its descendents. This instructs the xsl processor to copy everything from to i.e., the Member element and all its descendents.
9 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0"> See extension-example01
10 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Don’t forget extension-element- prefixes The extension-element-prefixes is used to tell the xsl processor, "whenever you encounter an element with any of these prefixes listed here you are to treat it as an extension element, and process it using the implementer's code" If you fail to do so the xsl processor will simply output the element literally (see extension-example02)
11 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Extension Functions We have seen some of the functions that XSL provides: substring(), contains(), substring-before, etc. Many xsl processor implementers provide additional functions. You signify that a function is an extension function by namespace qualifying it.
12 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Dynamic (run-time) Evaluation Many xsl processor implementers give you an extension function that enables you to dynamically evaluate an expression. –That is, you can generate the expression on the fly, or read it in from an external file. SAXON provides an extension function called evaluate to do this.
13 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. FitnessCenter.xsl FitnessCenter.xml XSL Processor checkFitnessCenter.xml results.xml This file contains expressions that are dynamically evaluated against FitnessCenter.xml Example: provide an xpath expression that ensures that each Member's level attribute is either Platinum or Gold, and nothing else.
14 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0"> <xsl:variable name="tests" select="document('checkFitnessCenter.xml')"/> SUCCEEDED FAILED Now any references is to elements in checkFitnessCenter.xml Takes us back to referencing elements in FitnessCenter.xml