Upload
natasha-delis
View
32
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Introduction to XSLT. Processing the XML data. Huge amount of XML information, and growing We need to “ manage ” it, and then “ process ” it Store it efficiently Verify the correctness Filter, search, select, join, aggregate Create new pieces of information Clean, normalize the data - PowerPoint PPT Presentation
Citation preview
Introduction to Introduction to XSLTXSLT
04/19/23 2
Processing the XML dataProcessing the XML data• Huge amount of XML information, and growingHuge amount of XML information, and growing• We need to “We need to “managemanage” it, and then “” it, and then “processprocess” it” it
• Store it efficientlyStore it efficiently• Verify the correctness Verify the correctness • Filter, search, select, join, aggregateFilter, search, select, join, aggregate• Create new pieces of informationCreate new pieces of information• Clean, normalize the dataClean, normalize the data • Update itUpdate it• Take actions based on the existing dataTake actions based on the existing data• Write complex execution flowsWrite complex execution flows
• No conceptual organization like for relational No conceptual organization like for relational databases (applications are too heterogeneous)databases (applications are too heterogeneous)
04/19/23 3
Frequent solutions to XML Frequent solutions to XML data managementdata management
1.1. Map it to Map it to genericgeneric programming APIs (e.g. programming APIs (e.g. DOM, SAX, StaX)DOM, SAX, StaX)
2.2. ManuallyManually map it to map it to non-genericnon-generic APIs APIs
3.3. AutomaticallyAutomatically map it to map it to non-genericnon-generic structuresstructures
4.4. Use Use XML extensionsXML extensions of existing languages of existing languages
5.5. ShreddingShredding for relational stores for relational stores
6.6. NativeNative XML processing through XSLT and XML processing through XSLT and XQueryXQuery
04/19/23 4
History of XSLTHistory of XSLT Much older then XQueryMuch older then XQuery
XSLT 1.0 and XSLT 2.0XSLT 1.0 and XSLT 2.0 Was designed a re-formatting language for the Was designed a re-formatting language for the
browsersbrowsers Still primarily used in this way (e.g Ebay has more 10.000 Still primarily used in this way (e.g Ebay has more 10.000
XSLT stylesheets)XSLT stylesheets) Most browsers have an embedded XSLT processorMost browsers have an embedded XSLT processor Now has broader applications for XML managementNow has broader applications for XML management
XSLT 2.0 and XQuery 1.0 are designed jointlyXSLT 2.0 and XQuery 1.0 are designed jointly Same data model, same type system, same Xpath 2.0Same data model, same type system, same Xpath 2.0 Different programming paradigmDifferent programming paradigm
XQuery is compositional and functional, XSLT is based on XQuery is compositional and functional, XSLT is based on recursive templatesrecursive templates
04/19/23 5
XQuery, Xpath, XSLTXQuery, Xpath, XSLT
Xpath 1.0
XSLT 2.0XQuery 1.0
Xpath 2.0
XSLT 1.0
uses
uses
extends, almost backwards compatible
extendsFLWOR expressionsNode constructorsValidation
1999
2007
04/19/23 6
Your first XSLT Your first XSLT stylesheetstylesheet
<<xsl:stylesheetxsl:stylesheet version="1.0" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict"> xmlns="http://www.w3.org/TR/xhtml1/strict">
<<xsl:templatexsl:template match="/"> match="/"><html> <html>
<head> <head> <title>Expense Report Summary</title> <title>Expense Report Summary</title>
</head> </head> <body> <body>
<p>Total Amount: <<p>Total Amount: <xsl:value-ofxsl:value-of select="expense- select="expense-report/total"/>report/total"/>
</p> </p> </body></body>
</html></html></</xsl:templatexsl:template>></</xsl:stylesheetxsl:stylesheet>>
04/19/23 7
The anatomy of a The anatomy of a stylesheetstylesheet An XSLT program is an XML document An XSLT program is an XML document
The root element of the document is called The root element of the document is called xsl:stylesheetxsl:stylesheet and is composed of a set of and is composed of a set of ““templatestemplates” (I.e elements called ” (I.e elements called xsl:templatexsl:template))
The The xslxsl namespace is bound to the “official” XSLT URI namespace is bound to the “official” XSLT URI (e.g. (e.g. http://www.w3.org/1999/XSL/Transform)http://www.w3.org/1999/XSL/Transform)
The XML elements composing the XSLT program: a The XML elements composing the XSLT program: a blend of “blend of “useruser” names and “” names and “XSLTXSLT” names (QNames in ” names (QNames in the the xslxsl namespace) namespace)
The “simple” The “simple” xslxsl elements are “interpreted” and elements are “interpreted” and replaced by the result of their evaluationreplaced by the result of their evaluation xsl:for-each, xsl:if, xsl:choose, xsl:value-ofxsl:for-each, xsl:if, xsl:choose, xsl:value-of
04/19/23 8
An XSLT programAn XSLT program<<xsl:stylesheetxsl:stylesheet version="1.0" version="1.0" xmlns:xsl=xmlns:xsl=http://www.w3.org/1999/XSL/Transformhttp://www.w3.org/1999/XSL/Transform>>
<<xsl:templatexsl:template …..> …..>………………</</xsl:templatexsl:template>>
<<xsl:templatexsl:template …..> …..>………………</</xsl:templatexsl:template>>
</</xsl:stylesheetxsl:stylesheet>>
Each template rule specifies how certain
nodes from the input XML doc have
to be reformatted in the output
04/19/23 9
XSLT templatesXSLT templates Rules that describe how certain input XML Rules that describe how certain input XML
nodes have to be transformed in the output nodes have to be transformed in the output nodesnodes
Represented by elements in the Represented by elements in the xslxsl namespace namespace called called <xsl:template><xsl:template>
Can have patterns that describe to what kind of Can have patterns that describe to what kind of nodes is the template rule applicablenodes is the template rule applicable <xsl:template <xsl:template match=“author | editormatch=“author | editor”>”>
Can have names (later)Can have names (later) Have a body -- an XML fragment that described Have a body -- an XML fragment that described
the outputthe output <xsl:template match=“*”> <xsl:template match=“*”> <foobar/><foobar/> <xsl:template> <xsl:template>
04/19/23 10
Template patternsTemplate patterns Describe to what kind of nodes is a template Describe to what kind of nodes is a template
applicable toapplicable to Represented as an optional Represented as an optional matchmatch attribute on attribute on
the the xsl:templatexsl:template elements elements The value of the match attribute is a string The value of the match attribute is a string
representing a patternrepresenting a pattern The pattern language is a subset of the XPath The pattern language is a subset of the XPath
languagelanguage A node A node matchesmatches a pattern if it is a member of a pattern if it is a member of
the result list of nodes of the pattern the result list of nodes of the pattern expression (almost normal XQuery evaluation)expression (almost normal XQuery evaluation)
04/19/23 11
Template patterns: Template patterns: examplesexamples
parapara matches any matches any parapara element element * matches any element* matches any element chapter|appendixchapter|appendix matches any matches any chapterchapter element and any element and any appendixappendix
elementelement olist/itemolist/item matches any matches any itemitem element with an element with an olistolist parent parent appendix//paraappendix//para matches any matches any parapara element with an element with an appendixappendix ancestor ancestor
elementelement // matches the root node matches the root node text()text() matches any text node matches any text node processing-instruction()processing-instruction() matches any processing instruction matches any processing instruction node()node() matches any node other than an attribute node and the root node matches any node other than an attribute node and the root node id("W11")id("W11") matches the element with unique ID matches the element with unique ID W11W11 para[1]para[1] matches any matches any parapara element that is the first element that is the first parapara child element of its child element of its
parentparent @class@class matches any matches any classclass attribute ( attribute (notnot any element that has a any element that has a classclass
attribute)attribute)•• @*@* matches any attribute matches any attribute
04/19/23 12
Applying a template to a Applying a template to a single nodesingle node
InputInput <foo>beam</foo><foo>beam</foo>
Two example templatesTwo example templates<<xsl:templatexsl:template match=“foo”> <bar>baz</bar> match=“foo”> <bar>baz</bar>
<bar>baz</bar><bar>baz</bar></</xsl:templatexsl:template>><<xsl:templatexsl:template match=“foo”> match=“foo”>
<bar><<bar><xsl:-value-ofxsl:-value-of select=“.”</ select=“.”</xsl:value-ofxsl:value-of></bar> ></bar> <bar>beam</bar><bar>beam</bar>
</</xsl:templatexsl:template>>
Applying a template to a single nodeApplying a template to a single node Return the body of the templateReturn the body of the template All the xsl elements in the body are “interpreted” and All the xsl elements in the body are “interpreted” and
replaced by their resultreplaced by their result The other elements remain unchangedThe other elements remain unchanged The current node is set to the input node while The current node is set to the input node while
evaluating Xpath expressions (remember “.”?)evaluating Xpath expressions (remember “.”?)
04/19/23 13
Recursive application of Recursive application of templatestemplates The templates are not (normally) invoked by The templates are not (normally) invoked by
hand (see later)hand (see later) XSLT semantics is based on a built-in, recursive XSLT semantics is based on a built-in, recursive
application of templatesapplication of templates Apply-templates( list of XML nodes) -> list of Apply-templates( list of XML nodes) -> list of
XML nodesXML nodes For each input node (in order, see later)For each input node (in order, see later)
Find the “best” template that applies (see conflicting Find the “best” template that applies (see conflicting templates later…)templates later…)
Note: choice of template is on a node basisNote: choice of template is on a node basis Apply the template, returns back a sequence of nodesApply the template, returns back a sequence of nodes
Concatenate all partial results, returnConcatenate all partial results, return The evaluation of the XSLT main program starts The evaluation of the XSLT main program starts
by invoking this recursive procedure on the input by invoking this recursive procedure on the input document nodedocument node
04/19/23 14
Invoking the recursive Invoking the recursive application of templatesapplication of templates
Why is this procedure “recursive” ?Why is this procedure “recursive” ? While evaluating a template one can trigger the While evaluating a template one can trigger the
re-evaluation of this procedurere-evaluation of this procedure <xsl:apply-template><xsl:apply-template>
Example:Example: Input Input
This is an <emph>important</emph> point.This is an <emph>important</emph> point. TemplateTemplate
<xsl:template match="emph"> <xsl:template match="emph"> <fo:inline-sequence font-weight="bold"> <fo:inline-sequence font-weight="bold">
<xsl:apply-templates select=“./text()”/> <xsl:apply-templates select=“./text()”/>
</fo:inline-sequence></fo:inline-sequence>
</xsl:template></xsl:template>
04/19/23 15
xsl:apply-templatesxsl:apply-templates Re-enter the built-in recursive application of Re-enter the built-in recursive application of
templatestemplates Has a Has a select select attribute that specifies on what set of attribute that specifies on what set of
nodes to apply the procedure (using Xpath)nodes to apply the procedure (using Xpath) <xsl:apply-templates select=“author”/><xsl:apply-templates select=“author”/> <xsl:apply-templates select=“author/name”/><xsl:apply-templates select=“author/name”/> <xsl:apply-templates select=“.//heading”/><xsl:apply-templates select=“.//heading”/> <xsl:apply-templates select=“ancestors::department/group”/><xsl:apply-templates select=“ancestors::department/group”/> <xsl:apply-templates select=“.”/><xsl:apply-templates select=“.”/>
The order of those nodes can be changed using a The order of those nodes can be changed using a xsl:sort (see later); default is document orderxsl:sort (see later); default is document order
If no If no selectselect attribute, then implicitly trigger the attribute, then implicitly trigger the recursive application of templates on the list of recursive application of templates on the list of children of the current nodechildren of the current node
04/19/23 16
Default templatesDefault templates What happens if there is no template that What happens if there is no template that
matches a node ? Default templates..matches a node ? Default templates.. Elements and document nodesElements and document nodes<xsl:template match="*|/"> <xsl:apply-templates/><xsl:template match="*|/"> <xsl:apply-templates/></xsl:template></xsl:template>
Attributes and text nodesAttributes and text nodes<xsl:template match="text()|@*"> <xsl:value-of <xsl:template match="text()|@*"> <xsl:value-of
select="."/></xsl:template>select="."/></xsl:template>
The other nodesThe other nodes<xsl:template match="processing-instruction()|<xsl:template match="processing-instruction()|
comment()"/>comment()"/>
04/19/23 17
Named templatesNamed templates Sometimes one can invoke a Sometimes one can invoke a
particular template -- by nameparticular template -- by name<xsl:template name=“authorsTemplate”><xsl:template name=“authorsTemplate”>
Instead of <xsl:apply-templates>Instead of <xsl:apply-templates> <xsl:call-template name=“<xsl:call-template name=“authorsTemplate”>authorsTemplate”>
Semantics is the sameSemantics is the same Small semantic differenceSmall semantic difference
xsl:call-templates does not change the current xsl:call-templates does not change the current nodenode
xsl:apply-templates doesxsl:apply-templates does
04/19/23 18
xsl:value-ofxsl:value-of You have seen it alreadyYou have seen it already <xsl:value-of select=“path expression”/><xsl:value-of select=“path expression”/> Evaluates the path expression => nodesEvaluates the path expression => nodes Apply the fn:string(..) function to each Apply the fn:string(..) function to each
node (remember it ?)node (remember it ?) Concatenate the stringsConcatenate the strings Create (and return) a new text node with Create (and return) a new text node with
this valuethis value
04/19/23 19
xsl:for-eachxsl:for-each<xsl:for-each select = node-set-expression> <!-- Content: (xsl:sort*, template-body) --></xsl:for-each>
The node-set expression evaluates to a list of nodes For each one of them return the template body, evaluated
normally Each application returns a list of nodes, concatenate them
all The input list is processed in document order in case of no
sort, otherwise in the sorting specified by the xsl:for-each
04/19/23 20
Xsl:for-each exampleXsl:for-each example DataData<customers> <customer> <name>...</name> <order>...</order> <order>...</order> </customer> <customer> <name>...</name> <order>...</order> <order>...</order> </customer></customers>
ProgramProgram<xsl:template match="/"> <html> <head> <title>Customers</title> </head> <body> <table> <tbody> <xsl:for-each select="customers/customer"> <tr> <th> <xsl:apply-templates select="name"/> </th> <xsl:for-each select="order"> <td> <xsl:apply-templates/> </td> </xsl:for-each> </tr> </xsl:for-each> </tbody> </table> </body> </html></xsl:template>
04/19/23 21
Xsl:ifXsl:if General formGeneral form
<xsl:if test = boolean-expression> <!-- Content: template-body --></xsl:if>
Example 1:<xsl:template match="namelist/name"> <xsl:apply-templates/> <xsl:if test="not(position()=last())">, </xsl:if></xsl:template>
Example 2:<xsl:template match="item"> <tr> <xsl:if test="position() mod 2 = 0"> <xsl:attribute name="bgcolor">yellow</xsl:attribute> </xsl:if> <xsl:apply-templates/> </tr></xsl:template>
04/19/23 22
Xsl:chooseXsl:choose
General form:General form:<xsl:choose> <!-- Content: (xsl:when+, xsl:otherwise?) --></xsl:choose>
<xsl:when test = boolean-expression> <!-- Content: template-body --></xsl:when>
<xsl:otherwise> <!-- Content: template-body --></xsl:otherwise>
04/19/23 23
xsl:choose: examplexsl:choose: example<xsl:template match="orderedlist/listitem"> <fo:list-item indent-start='2pi'> <fo:list-item-label> <xsl:variable name="level" select="count(ancestor::orderedlist) mod 3"/> <xsl:choose> <xsl:when test='$level=1'> <xsl:number format="i"/> </xsl:when> <xsl:when test='$level=2'> <xsl:number format="a"/> </xsl:when> <xsl:otherwise> <xsl:number format="1"/> </xsl:otherwise> </xsl:choose> <xsl:text>. </xsl:text> </fo:list-item-label> <fo:list-item-body> <xsl:apply-templates/> </fo:list-item-body> </fo:list-item></xsl:template>
04/19/23 24
xsl:sortxsl:sort Can be a child of xsl:apply-templates or xsl:for-Can be a child of xsl:apply-templates or xsl:for-
each elements; multiple xsl:sort possibleeach elements; multiple xsl:sort possible General form:General form:
<xsl:sort select = string-expression lang = { nmtoken } data-type = { "text" | "number" | qname-but-not-ncname } order = { "ascending" | "descending" } case-order = { "upper-first" | "lower-first" } />
Example:<xsl:template match="employees"> <ul> <xsl:apply-templates select="employee"> <xsl:sort select="name/family"/> <xsl:sort select="name/given"/> </xsl:apply-templates> </ul></xsl:template>
<xsl:template match="employee"> <li> <xsl:value-of select="name/given"/> <xsl:text> </xsl:text> <xsl:value-of select="name/family"/> </li></xsl:template>
04/19/23 25
Xsl:keyXsl:key Xsl:key allows to declare “keys” (unique identifiers Xsl:key allows to declare “keys” (unique identifiers
for elements belonging to a certain domain)for elements belonging to a certain domain) A key is composed of:A key is composed of:1. the name of the key (an expanded-name)
2. the set of nodes which has the key (the domain)3. the value of the key (a string)
General form:General form:<xsl:key name = qname match = pattern use = expression />
Can appear at the root of a stylesheet Use later using the key(Qname, expression) function Can be used to create cross-references between elements
(e.g. bibliographical references, HTML links, etc)
04/19/23 26
xsl:key: examplexsl:key: example One bib.xml document containing One bib.xml document containing
bibliographical entries:bibliographical entries:<entry name=”Java">...</entry>
One document containing references:One document containing references:<bibref>Java</bibref>
The stylesheet formatting the bibrefsThe stylesheet formatting the bibrefs<xsl:key name="bib" match="entry" use="@name"/>
<xsl:template match="bibref"> <xsl:variable name="name" select="."/> <xsl:for-each select="document('bib.xml')"> <xsl:apply-templates select="key('bib',$name)"/> </xsl:for-each></xsl:template>
04/19/23 27
Creating output: Creating output: elementselements Constant elements (similar to the direct element Constant elements (similar to the direct element
constructors in Xquery)constructors in Xquery)<xsl:if test=“a mod 2 = 0”><xsl:if test=“a mod 2 = 0”>
<foobar>baz</foobar><foobar>baz</foobar>
</xsl:if></xsl:if> Computed element generation (similar to the computed Computed element generation (similar to the computed
element constructors in Xquery)element constructors in Xquery) General form:General form:
<xsl:element name = { qname } namespace = { uri-reference } use-attribute-sets = qnames> <!-- Content: template-body --></xsl:element>
Example: <xsl:element name=“foobar”>baz</xsl:element> <xsl:element name=“foobar”><xsl:value-of select=“./text()”</xsl:value-
of></xsl:element>
04/19/23 28
Creating output: Creating output: attributesattributes
Computed attribute generation (similar to the Computed attribute generation (similar to the computed attribute constructors in XQuery)computed attribute constructors in XQuery)
General form:General form:<xsl:attribute name = { qname } namespace = { uri-reference } > <!-- Content: template-body --></xsl:attribute>
Example: <xsl:attribute name=“foobar”>baz</xsl:element> <xsl:attribute name=“foobar”><xsl:value-of
select=“./text()”</xsl:value-of></xsl:element>
04/19/23 29
Creating output: textCreating output: text
<xsl:value-of select=“expression”><xsl:value-of select=“expression”> <xsl:text> XXXX </xsl:text><xsl:text> XXXX </xsl:text>
04/19/23 30
Attribute value templatesAttribute value templates Similar to the dynamically computed content in the direct Similar to the dynamically computed content in the direct
attribute constructor in XQueryattribute constructor in XQuery <foo bar=“baz<foo bar=“baz{$x}{$x}”/>”/>
Unlike Xquery, only works for attributes (xsl:value-of does the Unlike Xquery, only works for attributes (xsl:value-of does the job for the elements)job for the elements)
Use “{“ and “}” like Xquery (or vice-versa..)Use “{“ and “}” like Xquery (or vice-versa..) ExampleExample<xsl:variable name="image-dir">/images</xsl:variable>
<xsl:template match="photograph"><img src="{$image-dir}/{href}" width="{size/@width}"/></xsl:template>
<photograph> <href>headquarters.jpg</href> <size width="300"/></photograph>
<img src="/images/headquarters.jpg" width="300"/>
04/19/23 31
xsl:copy and xsl:copy-ofxsl:copy and xsl:copy-of xsl:copy
Creates a shallow copy of the current node -- copying the shell (name +
NS) but not attributes and children<xsl:copy
use-attribute-sets = qnames>
<!-- Content: template-body -->
</xsl:copy>
Example -- the identity transformation<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
xsl:copy-of --- a way of copying entire XML tree fragments
04/19/23 32
Variables and Variables and parametersparameters General form:General form:
<xsl:variable name = qname select = expression> <!-- Content: template-body--></xsl:variable>
Similar to the LET in XquerySimilar to the LET in Xquery Variables are not modifiableVariables are not modifiable Scope is the body of the parent elementScope is the body of the parent element Examples:Examples:<xsl:variable name="n" select="2+2"/><foo bar=“{$n}”/>
<xsl:variable name="n">2</xsl:variable><xsl:value-of select="item[position()=$n]"/>
<xsl:variable name=“x”><xsl:apply-templates/></xsl:variable><xsl:copy-of select=“$x”/>
04/19/23 33
XQuery, Xpath, XSLTXQuery, Xpath, XSLT
Xpath 1.0
XSLT 2.0XQuery 1.0
Xpath 2.0
XSLT 1.0
uses
uses
extends, almost backwards compatible
extendsFLWOR expressionsNode constructorsValidation
1999
2007
04/19/23 34
XSLT vs. XQueryXSLT vs. XQuery XSLT has in addition to XQuery:XSLT has in addition to XQuery: Dynamically generated ns (2.0)Dynamically generated ns (2.0) Positional grouping (2.0)Positional grouping (2.0) Various output methods (HTML, XML, TEXT)Various output methods (HTML, XML, TEXT) Built-in dynamic dispatchBuilt-in dynamic dispatch
Xquery has lots of extra featuresXquery has lots of extra features Both are Turing completeBoth are Turing complete Some implementations use the same runtime for both languages, Some implementations use the same runtime for both languages,
and a simple different language layer (same data model, same and a simple different language layer (same data model, same Xpath, same libraries)Xpath, same libraries) E.g. Oracle, SaxonE.g. Oracle, Saxon
No standard way to cross-call between the two languages (future No standard way to cross-call between the two languages (future work)work)
Optimization:Optimization: Much easierMuch easier in Xquery because dataflow is possible. Not possible in XSLT in Xquery because dataflow is possible. Not possible in XSLT
(plus iterations are cheaper then recursive calls)(plus iterations are cheaper then recursive calls) Same reason why Xquery has a static typing and XSLT doesn’tSame reason why Xquery has a static typing and XSLT doesn’t
04/19/23 35
When to use each one ?When to use each one ? It depends on the applicationIt depends on the application Mix and match (see Pipelining in W3C)Mix and match (see Pipelining in W3C) Document formatting is better done in XSLTDocument formatting is better done in XSLT Data-style, query-style computations are better done in Data-style, query-style computations are better done in
XQueryXQuery ““Depends on the degree of entropy in the dataDepends on the degree of entropy in the data””
Irregular data => XSLTIrregular data => XSLT Regular data => XQueryRegular data => XQuery
Any side-effecting requirements imply XQuery (no updates Any side-effecting requirements imply XQuery (no updates and side-effects in XSLT)and side-effects in XSLT)
More then one document => almost implies XQueryMore then one document => almost implies XQuery Performance Performance willwill certainly be better in XQuery certainly be better in XQuery XQuery will be a richer language in the futureXQuery will be a richer language in the future XQuery widely present in universities, XSLT noXQuery widely present in universities, XSLT no Automatic translators between the two languagesAutomatic translators between the two languages
04/19/23 36
Frequent solutions to XML Frequent solutions to XML data managementdata management
1.1. Map it to Map it to genericgeneric programming APIs (e.g. programming APIs (e.g. DOM, SAX, StaX)DOM, SAX, StaX)
2.2. ManuallyManually map it to map it to non-genericnon-generic APIs APIs
3.3. AutomaticallyAutomatically map it to map it to non-genericnon-generic structuresstructures
4.4. Use Use XML extensionsXML extensions of existing languages of existing languages
5.5. ShreddingShredding for relational stores for relational stores
6.6. NativeNative XML processing through XSLT and XML processing through XSLT and XQueryXQuery