, , ( , , (

Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.

Similar presentations


Presentation on theme: "XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML."— Presentation transcript:

1 XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML document (or a part of it) –HTML document –or any other text document (although not designed to do so) An XSLT processor takes an XSLT stylesheet to convert an input document into the output document

2 XSLT stylesheet consists of template rules (xsl:template elements) each rule describes: –a pattern to match (e.g. element name) –set of parameters (optionally) –a sequence constructor: data (template) to output, typically some markup some data copied from source document some new data

3 Peter Kováč 1980-10-25 FMFI UK FiF UK Karol Nový 1982-11-16 FMFI UK <xsl:value-of select="FirstName"/>, <xsl:value-of select="LastName"/>, <xsl:value-of select="Study[1]/Faculty"/> (<xsl:value-of select="Study[1]/Faculty/@id"/>) Peter, Kováč, FMFI UK (1070) Karol, Nový, FMFI UK (1070)

4 Documents modeled as trees with following kinds of nodes: –the root (/) –element –attribute –text –namespace –processing instruction –comment

5 Basic algorithm XSLT processor accepts an input tree and produces an output tree Transformation is done by evaluating the sequence constructor of the initial template. –initial template: if not set explicitly, it is template rule selected for processing initial context node (default: root node) –other templates can be “called” from here Default templates: –for elements: call apply-templates on all child elements –for text and attributes: copy string value of node

6 Patterns – examples root node: / (relative) element name: e.g. Student, Person, FirstName, Study (relative) path: e.g. Person/Study, Person/Study/Faculty element at any level: e.g. Person//Faculty, //Study attribute: e.g. @id, Study/Faculty/@id wildcard: *, cdo:*, Person/*/Faculty, //*/@* “or” operator: FirstName|Lastname, *|@* test with [XPath expression]: –Person[FirstName=‘Peter’], –Faculty[@id=1070], –Study[position()=last()], –Person[Study/Faculty/@id=1050], –Person[Study]

7 Sequence constructors Sequence constructor can consist of –text nodes –literal result elements –XSLT instructions –extension instructions

8 XSLT instructions creating new nodes –xsl:document, xsl:element, xsl:attribute, xsl:text, xsl:value-of,... conditional or repeated execution –xsl:if, xsl:choose, xsl:for-each,... invoking other templates –xsl:apply-templates, xsl:next-match, xsl:call-template,... declaring variables –xsl:variable, xsl:param other

9 Creating new nodes a-text... a-value a-text

10 Conditional and repeated execution.........

11 Invoking other templates –finds a template for each node in the input sequence and invokes it –default: all child nodes (elements, PI’s, comments, text – not attributes!) –can be nodes that are not descendants

12 Variables definition (global/local):... use: $variable-name e.g.

13 Parameters declaration (at the level of stylesheet, template, or function): use like variables ($xxx) When invoking templates:

14 XPath 2.0 all values = sequences of –nodes, or –simple values, e.g. int, string, boolean, date single value = sequence of length 1 empty sequence: () single value: „a“ or („a“) general sequence: („a“, „b“, „c“) from www.saxonica.com

15 Constants string literals –"This is a test.“ –"This is a ‘test'." numeric constants –14 (integer) –385.032 (fixed point decimal) –3.2e-7 (double precision floating point number) no boolean constants –use true(), false() functions other –xs:dateTime("1966-07-31T15:00:00Z") –xs:float("10.7") –...

16 Various variables: $name function calls: fnc(arg1, arg2,...) comparisons / tests / conditionals: –for atomic values: eq, ne, lt, le, gt, ge –for sequences: =, !=,, >=: true if any pair has that relationship! –is (testing node identity) –deep-equal (node-sequence-1, node-sequence-2) – > (testing node precedentness in document order) –instance of, castable as –if (E1) then E2 else E3 –some/every $x in E1 satisfies E2

17 Accessing documents axis :: node-test axes available: –child (default), attribute (abbrev. @), ancestor, ancestor-or-self, descendant, descendant-or-self, preceding/following, preceding/following-sibling, parent, self node test - examples: –node name –prefix:*, *:localname –text(), node(), comment(), element(), element(Name), attribute(), attribute(Name)

18 Accessing documents (2) E1 / E2 / E3 means: –first we have an input sequence (of nodes), S1 –then we apply expression E1 on each of them, resulting in S2 –then we apply E2 on each of them, resulting in S3 –then we apply E3 on each of them, resulting in S4 e.g. –/Students/Person[@id=‘12998800’]/Study = /child::Students/child::Person[attribute::id=‘12998800’]/child::Study results in two elements “Study” –/Students/Person/Study (three elements “Study”) –/Students/Person[@id=‘12998800’]/FirstName/text() results in text node “Peter” –/Students/Person[@id=‘12998800’]/FirstName/string() results in string “Peter”

19 Peter Kováč 1980-10-25 FMFI UK FiF UK Karol Nový 1982-11-16 FMFI UK


Download ppt "XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML."

Similar presentations


Ads by Google