XSLT and Structural Recursion Gestão e Tratamento de Informação DEI IST 2011/2012
Outline Structural Recursion The XSLT Language
Structural Recursion : a different paradigm for processing data Data is modeled through sets and there is also a set union operator: {a:3, a:{b: one, c:5}, b:4} = {a:3} U {a:{b: one,c:5}} U {b:4} Data are processed by applying functions recursively over the structure (I.e., over the sets)
An Example Find all integer values in the data f($t1 U $T2) = f($t1) U f($t2) f({$l: $T}) = f($t) f({}) = {} f($v) = if isint($v) then {result: $V} else {} a a b 3 b c 4 one 5 result result result 3 5 4
Another Example What does the following program do? f($t1 U $T2) = f($t1) U f($t2) f({$l: $T}) = if $L=a then {b:f($t)} else {$L:f($T)} f({}) = {} f($v) = $V
Yet Another Example Increase engine prices by 10% f($t1 U $T2) = f($t1) U f($t2) f({$l: $T}) = if $L= engine then {$L: g($t)} else {$L: f($t)} f({}) = {} f($v) = $V g($t1 U $T2) = g($t1) U g($t2) g({$l: $T}) = if $L= price then {$L:1.1*$T} else {$L: g($t)} g({}) = {} g($v) = $V engine body engine body part price part price part price part price price 100 1000 price 100 1000 price 110 1100 price 100 1000
General form for structural recursion programs f 1 ($T1 U $T2) = f 1 ($T1) U f 1 ($T2) f 1 ({$L: $T}) = E 1 ($L, f 1 ($T),...,f k ($T), $T) f 1 ({}) = { } f 1 ($V) = g($v).... f k ($T1 U $T2) = f k ($T1) U f k ($T2) f k ({$L: $T}) = E k ($L, f 1 ($T),...,f k ($T), $T) f k ({}) = { } f k ($V) = g($v) Each E 1,..., E k consists only of {_ : _}, U, if_then_else_
Outline Structural Recursion The XSLT Language
The W3C XSL Standard The XSL standard is composed of: A language for expressing XML transformations (XSLT) An XML vocabulary for specifying formating semantics: XSL-FO (formatting objects) An XSLT program expresses a transformation from one XML document into a result tree: Another XML document An HTML document A document that contains FO XSL-FO in general: Corresponds to one or more screen areas or pages Has properties to describe the visual aspect of the area(s) Contains contents in the form of: text external objects (image, applet, etc.), or Additional formatting objects
XSLT Programs XSL program = template-rule... template-rule template-rule = match pattern + template Computation Model: Different from other query languages. XSL begins in the root element and tries to apply a pattern to ther node. If it succeeds, then it executes the corresponding template over the node. Normally, the template includes XSLT instructions for producing the XML result tree, or for applying templates recursively over the child nodes. In the last case, the process is repeated. Thus, he XSLT program is like a recursive function.
XSLT Template Rules The template rules are indicated by elements of the type xsl:template The matching pattern is specifyed through an XPath in the value for the match attribute The actual template is the content for the xsl:template element <xsl:apply-templates/> is an instruction that applies the entire XSLT program to all the descentants of the matched element
An Example XML Document <bib> </bib> <book> </book> <paper> </paper> <book> </book> <title> t1 </title> <author> a1 </author> <author> a2 </author> <title> t2 </title> <author> a3 </author> <author> a4 </author> <title> t3 </title> <author> a5 </author> <author> a6 </author> <author> a7 </author>
Example XSLT Program <xsl:template match = * > <xsl:apply-templates/> <xsl:template match = title > <result> <xsl:value-of select =. /> </result> Results: <result> t1 </result> <result> t2 </result> <result> t3 </result> <xsl:template match="text()">
How the program works It starts by the root <bib>... </bib> Checks if any pattern matches the root node The pattern from the first template is satisfied. XSLT evaluates the body of the template, which contains <xsl:apply-templates/> This implies that the entire program is going to be applied to all descendants of the <bib> element Once again, the pattern from the first template is satisfied This implies that the entire program is going to be applied to the elements <title> and <author> Finallty, the pattern for the second template is satisfied by the element <title> The XSLT program generates a <result>...</result> element in which the content is the value for the current node, i.e. The textual string with the title
XSLT Patterns: Path Expressions bib the bib element * any element / root /bib bib element under the root bib/paper paper element, child from bib bib//paper paper child from bib, at any depth //paper paper element at any depth paper book paper or book element @price price attribute bib/book/@price price attribute in book, from bib db/book[@price] books having a price attribute db/book[@price= 10 ] books with a price equal to 10
Using <xsl:element name= X > Creates a new element named X Example: <xsl:template match= A > <xsl:element name = B > <xsl:value-of select =. /> <xsl:element/> The above example is equivalent to: <xsl:template match= A > <B><xsl:value-of select =. /></B>
Another Example <xsl:template match= * > <xsl:element name = {name()} > <xsl:value-of select =. /> </xsl:element> Copies all top-level elements from the entry document name() returns the name for the current node, and we use this string as the name for the output node.
Another XSLT Program... <xsl:template match = * > <xsl:apply-templates/> <xsl:template match= a > <A><xsl:apply-templates/></A> <xsl:template match= b > <B><xsl:apply-templates/></B> <xsl:template match= c > <C><xsl:value-of select=. /></C>
...Applied to a Document <a> <e> <b> <c> 1 </c> <c> 2 </c> </b> <a> <c> 3 </c> </a> </e> <c> 4 </c> </a> <A> <B> <C> 1 </C> <C> 2 </C> </B> <A> <C> 3 </C> </A> <C> 4 </C> </A>
Conflict resolution for template matching rules If several template rules match, choose the one with the highest priority Priorities can either be: Explicit : <xsl:template match= abc priority= 1.41 > Implicit : ad-hoc rules given by W3C, based on the match match= abc priority 0. match= [... some namespace name... ] priority -0.25. match= node() priority -0.5.
XSLT and Structural Recursion Equivalent to: f(t1 U T2) = f(t1) U f(t2) f({l: T}) = if L= c then {C: t} else L= b then {B: f(t)} else L= a then {A: f(t)} else f(t) f({}) = {} f(v) = V <xsl:template match= c > <xsl:template match= b > <xsl:template match= a > <xsl:template match = * >
XSLT versus Structural Recursion The XSLT Language: Operates on trees Program can create an infinite loop Structural Recursion: Over arbitrary graphs Programs always terminate
Example : Converting XML to HTML <xsl:template match= / > <HTML> <HEAD> <TITLE> Bibliography entries </TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> <xsl:template match= title > <TD> <xsl:value-of select=. /> </TD> <xsl:template match= author > <TD><xsl:value-of select=. /></TD> <xsl:template match = book paper > <TR> <xsl:apply-templates select= title /> <xsl:apply-templates select= author /> </TR> <xsl:template match= bib > <TABLE> <TBODY> <xsl:apply-templates/> </TBODY> </TABLE>
HTML Result <HTML> <HEAD> <TITLE> Bibliography Entries </TITLE> </HEAD> <BODY> <TABLE> <TBODY> <TR><TD> t1 </TD> <TD> a1 </TD> <TD> a2 </TD> </TR> <TR><TD> t2 </TD> <TD> a3 </TD> <TD> a4 </TD> </TR> <TR><TD> t3 </TD> <TD> a5 </TD> <TD> a6 </TD> <TD> a7 </TD></TR> </TBODY> </TABLE> </BODY> </HTML>
Yet Another Simple XSLT Copies the entry document: <xsl:template match = / > <xsl:apply-templates/> <xsl:template match = {text()} > <xsl:value-of select=. /> <xsl:template match = * > <xsl:element name= name() > <xsl:apply-templates/> </xsl:element>
Exercise Write an XSLT program that transforms the XML document: <teaches> <teaches-tuple course="xml" lecturer="peter Wood"/> <teaches-tuple course="algorithms" lecturer="trevor Fenner"/> </teaches> Into another XML document with the following general schema: <teaches> <teaches-tuple> <course>xml</course><lecturer>peter Wood</lecturer> </teaches-tuple> <teaches-tuple> <course>algorithms</course><lecturer>trevor Fenner</lecturer> </teaches-tuple> </teaches>
References S. Abiteboul, P. Buneman, D. Suciu, Data on the Web, From Relations to Semistructured Data and XML, Morgan Kaufmann, 2000, (caps 5 e 6) Peter Wood, Slides on Representing and Querying Data on the Web, http://www.dcs.bbk.ac.uk/~ptw/teaching/data-on-the-web.html. Dan Suciu, Slides on The semistructured data model, CSE 590ds: Management of XML and Semistructured Data, http://www.cs.washington.edu/education/courses/cse590ds/01sp/ www.w3.org/style/xsl/ W3C's XSL home page nwalsh.com/docs/tutorials/xsl/ an XSL tutorial by Paul Grosso and Norman Walsh metalab.unc.edu/xml/books/bible/updates/14.html a chapter from the XML Bible on XSL Transformations (and XPath)
Questions? Gestão e Tratamento de Informação DEI IST 2011/2012