Download presentation
Presentation is loading. Please wait.
Published byCori Lang Modified over 6 years ago
1
Using Extension Elements and Extension Functions with XSLT and XPath
Roger L. Costello XML Technologies
2
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
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
Vendor-specific Each implementor gives the extension element a different name: saxon calls it: output xalan calls it: write
5
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: extension-element-prefixes="saxon" extension-element-prefixes="xalan" 3. Use the extension element: <saxon:output href="..."> -- anything in here will go to the file specified --- </saxon:output> <xalan:write file="..."> </xalan:write>
6
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
platinum.xml XSL FitnessCenter.xml Processor new-FitnessCenter.xml
<PlatinumMembers href="platinum.xml"/> <GoldMembers href="gold.xml"/> FitnessCenter.xml new-FitnessCenter.xml gold.xml FitnessCenter.xsl
8
<xsl:copy-of select="xpath"/>
This element instructs an xsl processor to copy to the output file the element selected by xpath, plus all its descendents. <xsl:template match="Member"> <xsl:copy-of select="."/> </xsl:template> This instructs the xsl processor to copy everything from <Member> to </Member> i.e., the Member element and all its descendents.
9
See extension-example01
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0"> <xsl:output method="xml"/> <xsl:template match="FitnessCenter"> <FitnessCenter> <PlatinumMembers href="platinum.xml"/> <saxon:output href="platinum.xml"> <PlatinumMembers> <xsl:for-each <xsl:copy-of select="."/> </xsl:for-each> </PlatinumMembers> </saxon:output> <GoldMembers href="gold.xml"/> <saxon:output href="gold.xml"> <GoldMembers> <xsl:for-each </GoldMembers> </FitnessCenter> </xsl:template> </xsl:stylesheet> See extension-example01
10
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
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
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
XSL Processor FitnessCenter.xml results.xml checkFitnessCenter.xml FitnessCenter.xsl 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
Now any references is to elements in checkFitnessCenter.xml
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" xmlns:saxon=" extension-element-prefixes="saxon" version="1.0"> <xsl:output method="xml"/> <xsl:variable name="tests" select="document('checkFitnessCenter.xml')"/> <xsl:template match="/"> <xsl:variable name="here" select="."/> <FitnessCenter-results> <xsl:for-each select="$tests//xpath"> <result> <xsl:variable name="xpath" select="."/> <xsl:value-of select="$xpath"/> <xsl:for-each select="$here"> <xsl:choose> <xsl:when test="saxon:evaluate($xpath)"> <xsl:text> SUCCEEDED</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text> FAILED</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:for-each> <xsl:text> </xsl:text> </result> </FitnessCenter-results> </xsl:template> </xsl:stylesheet> Now any references is to elements in checkFitnessCenter.xml Takes us back to referencing elements in FitnessCenter.xml
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.