Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any.

Similar presentations


Presentation on theme: "XSLT Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any."— Presentation transcript:

1 XSLT Part 3B

2 id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any attribute declared in DTD as having type ID The id() function provides an efficient means of locating nodes given the value of ID attribute. Example:

3 generate-id() The generate-id() function generates a string that uniquely identifies a node. Generate-id() can be used to create links in documents. Generate-id() can be used to compare whether two nodes are identical.

4 Keys The ID/IDREF mechanism for locating elements in XML documents has been generalized in XSL to the notion of keys. ID/IDREF is only useful when: –The document has declarations that identify the ID and IDREF attributes and –The processor is capable of processing the declarations Using select expressions (XPath) to locate elements may be inefficient Declaring keys gives the stylesheet processor an indication of what elements should be cached for fast access.

5 Club.xml Smith 555-1111 222-1212 Jones 123-4567 222-7777 Boggs 323-7892 222-4567

6 Examples Club Members Name …. Home Phone Number

7 Output Club Members Name Smith Jones Boggs Home Phone Number 555-1111 123-4567 323-7892

8 xsl:key Xsl:key is a top-level element used to declare a named key, for use with the key() functions in expressions and patterns. Global variables cannot be used in defining the key The name attribute specifies the name of the key. The match attribute is a pattern – specifies the nodes to which the key value applies. If a node matches the pattern, then the node will have zero or more values for the named key, as determined by use attribute. Example: Species an expression to determine the value or values of the key

9 Key() The key() function is used to find the nodes with a given value for a named key. It is used in conjunction with the element. The key() function is provided to make associative access to nodes more convenient and more efficient. Because keys provide an efficient way of retrieving all the nodes that share a common value, they are used to group nodes with common values.

10 Using an attribute for a key value <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="prodName" select ="key('prodId','7777')"/> Name: Price:

11 Using an element for a key value <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

12 Multi-valued keys A key can be multi-valued, in that a single node can have several values each of which can be used to find the node independently. The use expression, instructor/name is a node-set expression, so the string value of each of its nodes (each instructor name) is used as one of the values in the set of node value pairs that make up the key. Example

13 Course.xml CPP 123 Jones Smith Undergraduate 4 XML 345 Smith Mills graduate 2 Java 432 Boggs Pratt graduate 2 Output (All courses where Smith is an instructor): CPP 123 Jones Smith Undergraduate 4 XML 345 Smith Mills graduate 2

14 Example with catalog.xml, authors.xml, authors.xsl and books.xml Fun With XML John Robot 12367 Xml and Java Mary Jones John Robot 7856 C++ James Mills 7777 John Robot USA Mary Jones UK James Mills Australia

15 Authors.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

16 It is possible to have several keys for the same node. It is also possible to have several key definitions with the same name.

17 Stylesheet Reuse via xsl:include and xsl:import The elements xsl:include and xsl:import enable you to reuse other stylesheets. These elements are “top-level elements”. This means that they must be immediate children of the xsl:stylesheet element (i.e., they cannot be within a template rule) The xsl:include element is basically a macro substitution - the element is replaced by the contents of stylesheet it references

18 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">...... Replace the xsl:include element with the contents of the referenced stylesheet (i.e., all the children of xsl:stylesheet) toUpperCase.xsl

19 xsl:import xsl:import acts just like xsl:include - the stylesheet that it references is macro-substituted. However, there is a difference: –With xsl:include the stuff that is macro-substituted into the stylesheet has the same precedence as the rest of the stylesheet. It is as though you had one stylesheet. –With xsl:import the stuff that is macro-substituted into the stylesheet has lower precedence than the rest of the stylesheet. Also, all xsl:import elements must come first in the stylesheet.

20 Xsl:document The is used to create a new output file (introduced in XSLT1.1). The facility allows transformation to produce multiple output files. When the xsl:document instruction is instantiated, a new result tree is created and the new result tree becomes the current output destination for all the nodes output until the end of xsl:document element. The location of the new output file is determined by the value of href attribute. This attribute is mandatory. The href may contain an absolute or relative (to the parent document) URI.

21 xsl:document What is the output on the input file, club.xml

22 Input: Club.xml Smith 555-1111 222-1212 Jones 123-4567 222-7777 Boggs 323-7892 222-4567

23 Function- document() The most common usage of document() is to access a document referenced from the source document (typically in an attribute such as href). The document() finds an external XML document by resolving an URI reference, parses the XML into a tree structure and returns its tree node. Example: document(“example.xml”) looks for file example.xml in the same directory as the stylesheet, parses it and returns the root node of the resulting tree.

24 document() <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/ 1999/XSL/Transform"> Reference: Output: Reference: Logic In Maintenance This is a review of Logic in Maintenance Reference: XSLT Design Patterns This is a review of XSLT Design Patterns

25 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

26 The functions available in XPath cover only basic functionality. Sometimes, you may want to invoke code written in other languages from your stylesheet. The draft XSLT 1.1 specification defines a general mechanism for calling extension functions written in any language, and defines detailed interfaces for Java and Javascript. Interfaces for other languages may be defined by independent vendors.

27 When are extension functions needed To get data held in a database You may need to access services that are not directly available in XSLT or XPath. To perform complex operations that is cumbersome in XSLT.

28 Function-available() Using function-available() to test whether a particular function is available for use. Example: <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> True

29 Example <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://www.jclark.com/xt/java/java.util.Date ">

30 Example <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:Math="http://www.jclark.com/xt/java/java.lang.Math">

31 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

32 Vendor-specific Each implementor gives the extension element a different name: –saxon calls it: output –xalan calls it: write

33 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 ---

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

35 FitnessCenter.xsl FitnessCenter.xml XSL Processor gold.xml new-FitnessCenter.xsl platinum.xml

36 FitnessCenter.xml Jeff 555-1234 555-4321 lightgrey David 383-1234 383-4321 lightblue Roger 888-1234 888-4321 lightyellow

37 FitnessCenter.xsl ?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" version="1.0">

38 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

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

40 Example <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" version="1.0">

41 Database Connectivity Using Saxon To use the SQL extension elements in a stylesheet, you need to define a namespace prefix (for example "sql") in the extension-element-prefixes attribute of the xsl:stylesheet element. This extension defines four new stylesheet elements: sql:connect, sql:insert, sql:column, and sql:close: sql:connect creates a database connection. It has attributes "driver", "database", "user", and "password", all of which are attribute value templates (so the values can be passed in as parameters). The driver attribute names the JDBC driver class to be used. The database name must be name that JDBC can associate with an actual database, and in the sample stylesheet this database must contain a a table "Book" with three character columns, "Title", "Author", and "Category". sql:insert performs an SQL INSERT statement. This causes a row to be added to the table identified by the "table" attribute. sql:column is used as a child element of sql:insert, and identifies the name and value of a column to be included in the INSERT statement. The name of the column is identified by the "name" attribute, the value may be indicated either by evaluating the expression contained in the "select" attribute, or as the expanded contents of the sql:column element. The value is always interpreted as a String. (Remember this is purely a demonstration of extensibility, in a real system there would be a need to cater for SQL columns of other data types). sql:close closes the database connection.

42 To populate an Access database from Books.xml using Books- sql.xsl <xsl:stylesheet xmlns:sql="http://icl.com/saxon/extensions/com.icl.saxon.sql.SQ LElementFactory" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon"> sql:connect is not available Connecting to... <sql:connect driver="{$driver}" database="{$database}" user="{$user}" password="{$password}" xsl:extension-element-prefixes="sql"> SQL extensions are not installed

43 To populate an Access database from Books.xml using Books- sql.xsl


Download ppt "XSLT Part 3B. id() The id() function returns a node-set containing the node or nodes with a given ID attribute. An ID attribute in this context is any."

Similar presentations


Ads by Google