Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello.

Similar presentations


Presentation on theme: "1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello."— Presentation transcript:

1 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 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 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 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 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="http://icl.com/saxon" xalan: xmlns:xalan="http://org.apache.xalan.xslt.extensions.Redirect" 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 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 7 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. FitnessCenter.xsl FitnessCenter.xml XSL Processor gold.xml new-FitnessCenter.xml platinum.xml

8 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 9 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" version="1.0"> See extension-example01

10 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 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 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 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 14 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/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 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 16 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Problem Suppose that the document that we are processing is using namespaces: Jeff 555-1234 555-4321 lightgrey Note that we have a default namespace declaration. Thus, FitnessCenter, Member, Name, Phone, and FavoriteColor all belong to the http://www.gym.com namespace.

17 17 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Your name is: (see namespaces-example01) Output: -- empty --

18 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 http://www.gym.org namespace, i.e., {http://www.gym.com}Member

19 19 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Namespace Terminology {http://www.gym.com}Member Expanded name = The combination of the namespace URI and the local name Local name Namespace URI

20 20 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Namespace Terminology (cont.) … prefix

21 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 22 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Your name is: (see namespaces-example03) Output: Your name is: Jeff

23 23 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Alternatively <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gym="http://www.gym.com " 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 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 = http://www.gym.com Local name = Member Namespace URI = http://www.gym.com Local name = Name Namespace URI = http://www.gym.com Local name = Phone Namespace URI = http://www.gym.com... (see namespaces-example05)

25 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 26 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Jeff 555-1234 555-4321 lightgrey Local name = Namespace URI = Name =

27 27 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Output: Local name = FitnessCenter Namespace URI = http://www.gym.com Name = gym:FitnessCenter Local name = Member Namespace URI = http://www.gym.com Name = gym:Member Local name = Name Namespace URI = http://www.gym.com Name = gym:Name Local name = Phone Namespace URI = http://www.gym.com Name = gym:Phone Local name = Phone Namespace URI = http://www.gym.com Name = gym:Phone Local name = FavoriteColor Namespace URI = http://www.gym.com Name = gym:FavoriteColor (see namespaces-example06)

28 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 29 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. @* does not select namespace declarations! The @* will only select non-namespace declaration attributes. It will not select namespace declaration attributes <Library xmlns="http://www.library.org" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" id="Boston Public Library"> This will be selected by @* These will not be selected by @*

30 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 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 32 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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 33 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Error! Attempting to create an element in a namespace, but the namespace has not been declared yet!

34 34 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Simultaneously declare the element and its namespace Simultaneously declare the attribute and its namespace (see namespaces-example08)


Download ppt "1 Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension Functions with XSLT and XPath Roger L. Costello."

Similar presentations


Ads by Google