Copyright c 2002 Roger L Costello All Rights

  • Slides: 14
Download presentation
Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Using Extension Elements and Extension

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

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Extension Elements • The XSL

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 2

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Example Extension Element: instruct the

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! XML XSL Processor XSL 3

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Vendor-specific • Each implementor gives

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 4

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. How to use an extension

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: <saxon: output href=". . . "> -- anything in here will go to the file specified --</saxon: output> xalan: <xalan: write file=". . . "> -- anything in here will go to the file specified --</xalan: write> 5

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 6 Problem • Write a

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 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.

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 7 platinum. xml Fitness. Center.

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 7 platinum. xml Fitness. Center. xml XSL Processor <Platinum. Members href="platinum. xml"/> <Gold. Members href="gold. xml"/> new-Fitness. Center. xml gold. xml Fitness. Center. xsl

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <xsl: copy-of select="xpath"/> • This

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. <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. 8

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 9 <? xml version="1. 0"?

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 9 <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" xmlns: saxon="http: //icl. com/saxon" extension-element-prefixes="saxon" version="1. 0"> <xsl: output method="xml"/> <xsl: template match="Fitness. Center"> <Fitness. Center> <Platinum. Members href="platinum. xml"/> <saxon: output href="platinum. xml"> <Platinum. Members> <xsl: for-each select="Member[@level='platinum']"> <xsl: copy-of select=". "/> </xsl: for-each> </Platinum. Members> </saxon: output> <Gold. Members href="gold. xml"/> <saxon: output href="gold. xml"> <Gold. Members> <xsl: for-each select="Member[@level='gold']"> <xsl: copy-of select=". "/> </xsl: for-each> </Gold. Members> </saxon: output> </Fitness. Center> </xsl: template> </xsl: stylesheet> See extension-example 01

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Don’t forget extension-elementprefixes • The

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Don’t forget extension-elementprefixes • 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-example 02) 10

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Extension Functions • We have

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. 11

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Dynamic (run-time) Evaluation • Many

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. 12

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Fitness. Center. xml check. Fitness.

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. Fitness. Center. xml check. Fitness. Center. xml 13 XSL Processor results. xml Fitness. Center. xsl This file contains expressions that are dynamically evaluated against Fitness. Center. xml Example: provide an xpath expression that ensures that each Member's level attribute is either Platinum or Gold, and nothing else.

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 14 <? xml version="1. 0"?

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 14 <? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" xmlns: saxon="http: //icl. com/saxon" extension-element-prefixes="saxon" version="1. 0"> <xsl: output method="xml"/> <xsl: variable name="tests" select="document('check. Fitness. Center. xml')"/> <xsl: template match="/"> <xsl: variable name="here" select=". "/> <Fitness. Center-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> </result> </xsl: for-each> </Fitness. Center-results> </xsl: template> </xsl: stylesheet> Now any references is to elements in check. Fitness. Center. xml Takes us back to referencing elements in Fitness. Center. xml