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
15 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using XSLT and XPath to Transform XML Documents that contain Namespaces Roger L. Costello XML Technologies
16 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Problem Suppose that the document that we are processing is using namespaces: Jeff lightgrey Note that we have a default namespace declaration. Thus, FitnessCenter, Member, Name, Phone, and FavoriteColor all belong to the namespace.
17 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Your name is: (see namespaces-example01) Output: -- empty --
18 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Why is the output empty? Your name is: This template does not match any element in the instance document! This template matches on a Member element in no namespace. However, in our instance document the Member element is in the namespace, i.e., {
19 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Namespace Terminology { Expanded name = The combination of the namespace URI and the local name Local name Namespace URI
20 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Namespace Terminology (cont.) … prefix
21 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. local-name() This is a built-in function which returns a string, corresponding to the local name of the element. Local name = Output: Local name = FitnessCenter Local name = Member Local name = Name Local name = Phone Local name = FavoriteColor (see namespaces-example02)
22 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Your name is: (see namespaces-example03) Output: Your name is: Jeff
23 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Alternatively <xsl:stylesheet xmlns:xsl=" xmlns:gym=" " version="1.0"> Your name is: Declare the gym namespace Match on the Member element in the gym namespace Select the Name element in the gym namespace (see namespaces-example04)
24 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. namespace-uri() This is a built-in function which returns a string corresponding to the namespace URI of the node. Local name = Namespace URI = Output: Local name = FitnessCenter Namespace URI = Local name = Member Namespace URI = Local name = Name Namespace URI = Local name = Phone Namespace URI = (see namespaces-example05)
25 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. name() Revisited We have seen the name() function before. It returns the name of the node. But what name does it return if the node is in a namespace? –Answer: it returns the element name and its prefix (this is called the QName, for Qualified Name)
26 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Jeff lightgrey Local name = Namespace URI = Name =
27 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Output: Local name = FitnessCenter Namespace URI = Name = gym:FitnessCenter Local name = Member Namespace URI = Name = gym:Member Local name = Name Namespace URI = Name = gym:Name Local name = Phone Namespace URI = Name = gym:Phone Local name = Phone Namespace URI = Name = gym:Phone Local name = FavoriteColor Namespace URI = Name = gym:FavoriteColor (see namespaces-example06)
28 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Identity transform - copying namespace declarations Recall our identity transform stylesheet: Iterate through each attribute and add them as attributes onto the element.
29 Copyright (c) [2002]. Roger L. Costello. All Rights does not select namespace declarations! will only select non-namespace declaration attributes. It will not select namespace declaration attributes <Library xmlns=" xmlns:xsi=" id="Boston Public Library"> This will be selected These will not be selected
30 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Identity transformation for XML documents containing namespaces? So how do we create a stylesheet that can copy over namespace declarations, along with the other attributes? –Answer: use the element
31 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. This element will copy the current element and all namespace declarations to the output file. Shallow copy (copy current node) Cf: –Deep copy (Copy current tree ) –copy all attributes and namespace nodes –copy all descendants
32 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> (see namespaces-example07) The problem with this identity transform stylesheet is that it's not set up to allow us to make changes to elements/attributes.
33 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Error! Attempting to create an element in a namespace, but the namespace has not been declared yet!
34 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl=" version="1.0"> Simultaneously declare the element and its namespace Simultaneously declare the attribute and its namespace (see namespaces-example08)