Title: XSLT
1XSLT
2id()
- 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
- ltxslfor-each selectdocument(example.xml)"gt
- ltxslvalue-of selectid(A123)"/gt
- lt/xslfor-eachgt
3generate-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.
4Keys
- 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.
5Club.xml
- lt?xml version"1.0"?gt
- ltClubgt
- ltMember id"1"gt
- ltNamegtSmithlt/Namegt
- ltPhone type"home"gt555-1111lt/Phone
gt - ltPhone type"work"gt222-1212lt/Phone
gt -
- lt/Membergt
- ltMember id"2"gt
- ltNamegtJoneslt/Namegt
- ltPhone type"home"gt123-4567lt/Phone
gt - ltPhone type"work"gt222-7777lt/Phone
gt -
- lt/Membergt
- ltMember id"3" gt
- ltNamegtBoggslt/Namegt
- ltPhone type"home"gt323-7892lt/Phone
gt - ltPhone type"work"gt222-4567lt/Phone
gt
6Examples
- ltxsloutput method"html"/gt
- ltxsltemplate match"/"gt
- ltHTMLgtltHEAD ltTITLEgtClub
Memberslt/TITLEgtlt/HEADgtltBODYgtltTABLE border"1"
width"25"gt - ltTRgtltTHgtNamelt/THgtlt/TRgt
- ltxslfor-each
select"/Club/Member"gt - ltTRgt
- ltTDgt
- ltA
href"generate-id()"gt - ltxslvalue-of
select"Name"/gt - lt/Agt
- lt/TDgt
- lt/TRgt
- lt/xslfor-eachgt
- lt/TABLEgt
- .
- ltTABLE border"1" width"25"gt
- ltTRgtltTHgtHome Phone
Numberlt/THgtlt/TRgt - ltxslfor-each
select"/Club/Member"gt
7Output
- ltHTMLgt
- ltHEADgt
- ltmeta http-equiv"Content-Type"
content"text/html charsetutf-8"gt - ltTITLEgtClub Memberslt/TITLEgt
- lt/HEADgt
- ltBODYgtltTABLE border"1" width"25"gt
- ltTRgtltTHgtNamelt/THgt
- lt/TRgt
- ltTRgtltTDgtltA href"d0e4"gtSmithlt/Agtlt/TDgtlt/T
Rgt - ltTRgtltTDgtltA href"d0e16"gtJoneslt/Agtlt/TDgtlt/
TRgt - ltTRgtltTDgtltA href"d0e28"gtBoggslt/Agtlt/TDgt
- lt/TRgt
- lt/TABLEgtltBRgtltBRgtltBRgtltBRgtltBRgtltBRgtltBRgtltBRgtltBRgt
ltBRgtltTABLE border"1" width"25"gt - ltTRgt
- ltTHgtHome Phone Numberlt/THgt
- lt/TRgt
- ltTRgtltTDgtltA name"d0e4"gt555-1111lt/Agtlt/TDgtlt
/TRgt - ltTRgtltTDgtltA name"d0e16"gt123-4567lt/Agtlt/TDgt
lt/TRgt - ltTRltTDgtltA name"d0e28"gt323-7892lt/Agtlt/TDgtlt
/TRgt
8xslkey
- Xslkey 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
- ltxslkey name"prodId" match"product"
use"_at_code"/gt
Species an expression to determine the value or
values of the key
9Key()
- The key() function is used to find the nodes with
a given value for a named key. - It is used in conjunction with the ltxslkeygt
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.
10Using an attribute for a key value
- lt?xml version"1.0" ?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
- lt!-- Using an attribute for a key value --gt
- ltxslkey name"prodId" match"product"
use"_at_code"/gt - ltxsltemplate match"/"gt
- lthtmlgt
- ltbodygt
- ltxslvariable name"prodName"
- select"key('prodId','7777')"/gt
- ltpgt
- Name ltxslvalue-of select"prodName/description
"/gtltbr /gt - Price ltxslvalue-of select"prodName/price"
/gtltbr /gt - lt/pgt
- lt/bodygt
- lt/htmlgt
- lt/xsltemplategt
11Using an element for a key value
- lt?xml version"1.0" ?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
- lt!-- Using an element as a key --gt
- ltxslkey name"courseKey" match"course"
use"courseId"/gt - ltxslparam name"cid" select"345"/gt
- ltxsltemplate match"/"gt
- lthtmlgt
- ltbodygt
- ltxslapply-templates select"key('courseKey',ci
d)"/gt - lt/bodygt
- lt/htmlgt
- lt/xsltemplategt
12Multi-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
- ltxslkey name"cinst" match"course
use"instructor/name"/gt - ltxsltemplate match"/"gt
- lthtmlgt
- ltbodygt
- ltxslapply-templates select"key('cinst','Smith'
)"/gt - lt/bodygt
- lt/htmlgt
- lt/xsltemplategt
- lt/xslstylesheetgt
13Course.xml
- lt?xml version"1.0"?gt
- ltcoursesgt
- ltcoursegt
- lttitlegtCPPlt/titlegt
- ltcourseIdgt123lt/courseIdgt
- ltinstructorgt
- ltnamegtJoneslt/namegt
- ltnamegtSmithlt/namegt
- lt/instructorgt
- ltlevelgtUndergraduatelt/levelgt
- ltunitsgt4lt/unitsgt
- lt/coursegt
- ltcoursegt
- lttitlegtXMLlt/titlegt
- ltcourseIdgt345lt/courseIdgt
- ltinstructorgt
- ltnamegtSmithlt/namegt
- ltnamegtMillslt/namegt
- lt/instructorgt
- Output (All courses where Smith is an
instructor) - CPP 123 Jones Smith Undergraduate 4
- XML 345 Smith Mills graduate 2
14Example with catalog.xml, authors.xml,
authors.xsl and books.xml
- lt?xml version"1.0"?gt
- ltauthorsgt
- ltauthorgt
- ltnamegtJohn Robotlt/namegt
- ltcountrygt USAlt/countrygt
-
- lt/authorgt
- ltauthorgt
-
- ltnamegtMary Joneslt/namegt
- ltcountrygt UKlt/countrygt
-
- lt/authorgt
- ltauthorgt
- ltnamegtJames Millslt/namegt
- ltcountrygt Australialt/countrygt
- lt/authorgt
- lt?xml version"1.0"?gt
- ltcataloggt
- ltbookgt
- lttitle language"English"gtFun With XMLlt/titlegt
- ltauthorgtJohn Robotlt/authorgt
- ltisbngt 12367lt/isbngt
-
- lt/bookgt
- ltbookgt
- lttitle language"English"gtXml and Javalt/titlegt
- ltauthorgtMary Joneslt/authorgt
- ltauthorgtJohn Robotlt/authorgt
- ltisbngt 7856lt/isbngt
-
- lt/bookgt
- ltbookgt
- lttitle language"English"gtClt/titlegt
15Authors.xsl
- lt?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" - version"1.0"gt
- ltxslkey name"biog" match"author" use"name"/gt
- ltxslvariable name"biogs" select"document('a
uthors.xml')"/gt - ltxsltemplate match"/"gt
- ltHTMLgt
- ltBODYgt
- ltxslvariable name"allBooks"
select"//book"/gt - ltxslfor-each select"allBooks"gt
- ltH1gtltxslvalue-of select"title"/gtlt/H1gt
- ltxslfor-each select"author"gt
- ltxslvariable name"name" select"."/gt
- ltH3gtltxslvalue-of select"name"/gtlt/H3gt
- ltxslfor-each select"biogs"gt
- ltxslvariable name"auth" select"key('biog',n
ame)"/gt - ltpgtltxslvalue-of select"auth/country"/gtlt/pgt
- ltpgtltxslvalue-of select"auth/biog"/gtlt/pgt
- lt/xslfor-eachgt
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.
17Stylesheet Reuse via xslinclude and xslimport
- The elements xslinclude and xslimport enable
you to reuse other stylesheets. - These elements are top-level elements. This
means that they must be immediate children of the
xslstylesheet element (i.e., they cannot be
within a template rule) - The xslinclude element is basically a macro
substitution - the element is replaced by the
contents of stylesheet it references
18lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxslinclude
href"file//localhost/xml-course/new-xsl/toUpperC
ase.xsl"/gt ltxsltemplate match"FitnessCenter
"gt ... lt/xsltemplategt
... lt/xslstylesheetgt
lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxslvariable
name"lcase" select"'abcdefghijklmnopqrstuvwxyz'"
/gt ltxslvariable name"ucase" select"'ABCDEFGHIJK
LMNOPQRSTUVWXYZ'"/gt ltxsltemplate match""gt
ltxslapply-templates select"_at_ text()
comment() processing-instruction()"/gt lt/xsltemp
lategt ltxsltemplate match"_at_"gt ltxslvalue-of
select"translate(.,lcase, ucase)"/gt lt/xsltempl
ategt ltxsltemplate match"text()"gt
ltxslvalue-of select"translate(.,lcase,
ucase)"/gt lt/xsltemplategt lt/xslstylesheetgt
Replace the xslinclude element with the
contents of the referenced stylesheet (i.e., all
the children of xslstylesheet)
toUpperCase.xsl
19xslimport
- xslimport acts just like xslinclude - the
stylesheet that it references is
macro-substituted. However, there is a
difference - With xslinclude 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 xslimport the stuff that is
macro-substituted into the stylesheet has lower
precedence than the rest of the stylesheet.
Also, all xslimport elements must come first in
the stylesheet.
20Xsldocument
- The ltxsldocumentgt is used to create a new output
file (introduced in XSLT1.1). - The facility allows transformation to produce
multiple output files. - When the xsldocument 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 xsldocument 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.
21xsldocument
- ltxsltemplate match"Member"gt
- ltMembergt
- ltxslcopy-of select"Name"/gt
- ltxslapply-templates select"Phone"/gt
- lt/Membergt
- lt/xsltemplategt
- ltxsltemplate match"Phone"gt
- ltxslvariable name"phonebook"
href"PhoneNumbers.xml"/gt - ltPhone href"phonebook"/gt
- ltxsldocument href"PhoneNumbers.xml"gt
- ltxslcopy-of select"."/gt
- lt/xsldocumentgt
- lt/xsltemplategt
- lt/xslstylesheetgt
- What is the output on the input file, club.xml
22Input Club.xml
- lt?xml version"1.0"?gt
- ltClubgt
- ltMembergt
- ltNamegtSmithlt/Namegt
- ltPhone type"home"gt555-1111lt/Phone
gt - ltPhone type"work"gt222-1212lt/Phone
gt -
- lt/Membergt
- ltMembergt
- ltNamegtJoneslt/Namegt
- ltPhone type"home"gt123-4567lt/Phone
gt - ltPhone type"work"gt222-7777lt/Phone
gt -
- lt/Membergt
- ltMembergt
- ltNamegtBoggslt/Namegt
- ltPhone type"home"gt323-7892lt/Phone
gt - ltPhone type"work"gt222-4567lt/Phone
gt -
23Function- 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.
24document()
- lt?xml version"1.0"?gt
- ltcataloggt
- ltpaper gt
- ltreference title"Logic In Maintenance"
link"review1.xml"/gt - ltreference title"XSLT Design Patterns"
link"review2.xml"/gt - lt/papergt
-
- lt/cataloggt
- 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
- lt?xml version"1.0" ?gt
- ltxslstylesheet version"1.1"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
- ltxsltemplate match"paper"gt
-
- ltxslfor-each select"reference"gt
- lth2gtReference ltxslvalue-of select"_at_title"/gtlt/h
2gt - ltxslapply-templates select"document(_at_link)"/gt
-
- lt/xslfor-eachgt
-
-
- lt/xsltemplategt
- lt/xslstylesheetgt
25Extension Elements
- The XSL processor understands how to process
xsltemplate, xslapply-templates, xslif,
xslfor-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.
27When 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.
28Function-available()
- Using function-available() to test whether a
particular function is available for use. - Example
- lt?xml version"1.0" ?gt
- ltxslstylesheet version"1.1"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
- ltxsltemplate match"/"gt
- ltxslif test"function-available('key')"gt
- True
- lt/xslifgt
- lt/xsltemplategt
- lt/xslstylesheetgt
29Example
- ltxslstylesheet
- version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
- xmlnsdate"http//www.jclark.com/xt/java/java.u
til.Date"gt - ltxsltemplate match"/"gt
- lthtmlgt
- ltxslif test"function-available('dateto-stri
ng') and function-available('datenew')"gt - ltpgtltxslvalue-of select"dateto-string(date
new())"/gtlt/pgt - lt/xslifgt
- lt/htmlgt
- lt/xsltemplategt
- lt/xslstylesheetgt
30Example
- ltxslstylesheet
- version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
- xmlnsMath"http//www.jclark.com/xt/java/java.l
ang.Math"gt - ltxsltemplate match"/"gt
- lthtmlgt
-
- ltxslif test"function-available('Mathmax')"gt
- ltpgtltxslvalue-of select"Mathmax(number('12
'),number('13'))"/gtlt/pgt - lt/xslifgt
-
- lt/htmlgt
- lt/xsltemplategt
- lt/xslstylesheetgt
31Example 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
32Vendor-specific
- Each implementor gives the extension element a
different name - saxon calls it output
- xalan calls it write
33How to use an extension element
1. Declare the namespace that the extension
element belongs to saxon
xmlnssaxon"http//icl.com/saxon"
xalan xmlnsxalan"http//org.apach
e.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
ltsaxonoutput file"..."gt
-- anything in here will go to the file
specified --- lt/saxonoutputgt
xalan ltxalanwrite
file"..."gt -- anything in here
will go to the file specified ---
lt/xalanwritegt
34Problem
- 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.
35platinum.xml
XSL Processor
ltPlatinumMembers href"platinum.xml"/gt ltGoldMember
s href"gold.xml"/gt
FitnessCenter.xml
new-FitnessCenter.xsl
gold.xml
FitnessCenter.xsl
36FitnessCenter.xml
- lt?xml version"1.0"?gt
- lt?xml-stylesheet type"text/xsl"
href"FitnessCenter.xsl"?gt - ltFitnessCentergt
- ltMember level"platinum"gt
- ltNamegtJefflt/Namegt
- ltPhone type"home"gt555-1234lt/Phone
gt - ltPhone type"work"gt555-4321lt/Phone
gt - ltFavoriteColorgtlightgreylt/Favorite
Colorgt - lt/Membergt
- ltMember level"gold"gt
- ltNamegtDavidlt/Namegt
- ltPhone type"home"gt383-1234lt/Phone
gt - ltPhone type"work"gt383-4321lt/Phone
gt - ltFavoriteColorgtlightbluelt/Favorite
Colorgt - lt/Membergt
- ltMember level"platinum"gt
- ltNamegtRogerlt/Namegt
- ltPhone type"home"gt888-1234lt/Phone
gt - ltPhone type"work"gt888-4321lt/Phone
gt
37FitnessCenter.xsl
- ?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" - xmlnssaxon"http//icl.com/saxon"
- extension-element-prefixes"saxon"
- version"1.0"gt
- ltxsloutput method"xml"/gt
- ltxsltemplate match"FitnessCenter"gt
- ltFitnessCentergt
- ltPlatinumMembers href"platinum.xml"/gt
- ltsaxonoutput href"platinum.xml"gt
- ltPlatinumMembersgt
- ltxslfor-each
select"Member_at_level'platinum'"gt - ltxslcopy-of select"."/gt
- lt/xslfor-eachgt
- lt/PlatinumMembersgt
- lt/saxonoutputgt
- ltGoldMembers href"gold.xml"/gt
- ltsaxonoutput href"gold.xml"gt
- ltGoldMembersgt
38extension-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
39Dynamic (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.
40Example
- lt?xml version"1.0"?gt
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" - xmlnssaxon"http//icl.com/saxon"
- extension-element-prefixes"saxon"
- version"1.0"gt
-
- ltxsloutput method"xml"/gt
- ltxslvariable name"elementName"
select"'title'"/gt -
- ltxsltemplate match"/"gt
- ltxslvariable name"check" select"saxonevaluat
e(concat('//book/',elementName))"/gt - ltxslvalue-of select"check"/gt
-
- lt/xsltemplategt
- lt/xslstylesheetgt
41Database ConnectivityUsing Saxon
- To use the SQL extension elements in a
stylesheet, you need to define a namespace prefix
(for example "sql") in the extension-element-prefi
xes attribute of the xslstylesheet element. - This extension defines four new stylesheet
elements sqlconnect, sqlinsert, sqlcolumn,
and sqlclose - sqlconnect 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". - sqlinsert performs an SQL INSERT statement. This
causes a row to be added to the table identified
by the "table" attribute. - sqlcolumn is used as a child element of
sqlinsert, 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 sqlcolumn 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). - sqlclose closes the database connection.
42To populate an Access database from Books.xml
using Books-sql.xsl
- ltxslstylesheet
- xmlnssql"http//icl.com/saxon/extensions/com.ic
l.saxon.sql.SQLElementFactory" - xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0" - xmlnssaxon"http//icl.com/saxon"
- extension-element-prefixes"saxon"gt
- lt!-- insert your database details here, or supply
them in parameters --gt - ltxslparam name"driver" select"'sun.jdbc.odbc.Jd
bcOdbcDriver'"/gt - ltxslparam name"database" select"'jdbcodbctest
'"/gt - ltxslparam name"user"/gt
- ltxslparam name"password"/gt
- ltxsltemplate match/"gt
- ltxslif test"not(element-available('sqlconne
ct'))"gt - ltxslmessagegtsqlconnect is not
availablelt/xslmessagegt - lt/xslifgt
- ltxslmessagegtConnecting to ltxslvalue-of
select"database"/gt...lt/xslmessagegt - ltsqlconnect driver"driver"
database"database" - user"user"
password"password" - xslextension-element-prefixes"sql"gt
- ltxslfallbackgt
43To populate an Access database from Books.xml
using Books-sql.xsl
- ltxsltemplate matchcatalog"gt
- ltxslfor-each selectbook"gt
- ltsqlinsert table"book" xslextension-elemen
t-prefixes"sql"gt - ltsqlcolumn name"title" select"TITLE"/gt
- ltsqlcolumn name"author"
select"AUTHOR"/gt - ltsqlcolumn name"category"
select"_at_CAT"/gt - lt/sqlinsertgt
- lt/xslfor-eachgt
- lt/xsltemplategt
- lt/xslstylesheetgt