Presentation is loading. Please wait.

Presentation is loading. Please wait.

SDPL 2005Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support for –arithmetics –processing ID/IDREF cross-references.

Similar presentations


Presentation on theme: "SDPL 2005Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support for –arithmetics –processing ID/IDREF cross-references."— Presentation transcript:

1 SDPL 2005Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support for –arithmetics –processing ID/IDREF cross-references –manipulation of strings n Generating text –for content –for attribute values n Repetition, sorting and conditional processing n Generating numbers

2 SDPL 2005Notes 5.1: Additional XPath & XSLT2 XPath: Arithmetical Operations Operators for double-precision (64 bit) floating- point arithmetics +, -, *, div, mod (same as % in Java) Operators for double-precision (64 bit) floating- point arithmetics +, -, *, div, mod (same as % in Java) Mapping numbers to integers: Mapping numbers to integers: »floor(x) = max{n  n  x} »ceiling(x) = min{n  n  x} »round(x) = floor(x+0.5) Formatting numbers as strings (example): Formatting numbers as strings (example): »format-number( , " 0.0 " ) = " -1.3 "

3 SDPL 2005Notes 5.1: Additional XPath & XSLT3 Aggregate Functions n Counting nodes »count( node-set ) –and summing them as numbers »sum( node-set ) n Example: –Grade average for the first student in document: div count((//student)[1]/course)

4 SDPL 2005Notes 5.1: Additional XPath & XSLT4 Cross-referencing Function id selects elements by their unique ID Function id selects elements by their unique ID –NB: ID attributes need to be declared in DTD (See an example later) n Examples: –id('sect:intro') selects the element with unique ID "sect:intro" –id('sect:intro')/para[5] selects the fifth para child of the above element –id('sect1 sect2 sect3') selects 3 sections (if they have the corresponding ID values)

5 SDPL 2005Notes 5.1: Additional XPath & XSLT5 String manipulation Equality and inequality of strings can be tested with operators = and != Equality and inequality of strings can be tested with operators = and != –"foo" = 'foo' ; "foo" != "Foo" Testing for substrings: Testing for substrings: –starts-with("dogbert", "dog") = true() –contains("dogbert", "gbe") = true() n Concatenation (of two or more strings), –concat("dog", "bert") = "dogbert"

6 SDPL 2005Notes 5.1: Additional XPath & XSLT6 XPath: more string functions –substring-before("ftp://a", "//") = substring-before("ftp://a", "/") = "ftp:" –substring-after("ftp://a", "/")= "/a" –substring( string, start, length? ): »substring("dogbert", 1, 3) = "dog" »substring("dogbert", 3) = "gbert" –string-length("dogbert")=7 –translate( Str, Replaced, Replacing ): »translate("doggy","dgo","Ssi")="Sissy"

7 SDPL 2005Notes 5.1: Additional XPath & XSLT7 Generating Text The string-value of an expression can be inserted in the result tree by instruction The string-value of an expression can be inserted in the result tree by instruction –if Expr evaluates to a node-set, value of the first node in document order is used n Consider transforming source elements like Charlie Parker Charlie Parker to the form Charlie ("Bird") Parker

8 SDPL 2005Notes 5.1: Additional XPath & XSLT8 Computing generated text (2) This can be specified by template rule This can be specified by template rule (" ") (" ") Verbatim text (like the white-space above) can be inserted using xsl:text Verbatim text (like the white-space above) can be inserted using xsl:text

9 SDPL 2005Notes 5.1: Additional XPath & XSLT9 Attribute value templates The string-value of an expression can be inserted in an attribute value by surrounding the expression by braces { and } The string-value of an expression can be inserted in an attribute value by surrounding the expression by braces { and } Consider transforming source element Consider transforming source element Mary.jpg Mary.jpg into form into form

10 SDPL 2005Notes 5.1: Additional XPath & XSLT10 Attribute value templates (2) Attribute value templates (2) This can be specified by template rule This can be specified by template rule Expressions {file} and are evaluated in the context of the current node (the photo element) Expressions {file} and are evaluated in the context of the current node (the photo element)

11 SDPL 2005Notes 5.1: Additional XPath & XSLT11 XSLT: Repetition Nodes can be "pulled" from source for processing using Template Nodes can be "pulled" from source for processing using Template –the template is applied to each of the selected nodes (0, 1 or more), each node in turn as the current() node »in document order, unless sorted using xsl:sort instructions (see later)

12 SDPL 2005Notes 5.1: Additional XPath & XSLT12 Example (of xsl:for-each ) n Consider formatting the below document as HTML: ]> The Joy of XML Getting Started Helen Brown says that processing XML documents is fun. Dave Dobrik agrees. Family affairs Bob Brown is the husband of Helen Brown. Finishing Up As we discussed in, processing XML documents is fun. ]> The Joy of XML Getting Started Helen Brown says that processing XML documents is fun. Dave Dobrik agrees. Family affairs Bob Brown is the husband of Helen Brown. Finishing Up As we discussed in, processing XML documents is fun.

13 SDPL 2005Notes 5.1: Additional XPath & XSLT13 Example: Table of contents n A table of contents can be formed of section titles: Table of Contents Table of Contents

14 SDPL 2005Notes 5.1: Additional XPath & XSLT14 Example (cont; Cross references) Cross references (to sections) can also be processed using xsl:for-each: Cross references (to sections) can also be processed using xsl:for-each: Section (...) Section (...) With this rule the source fragment With this rule the source fragment As we discussed in As we discussed in becomes As we discussed in Section (Getting …)

15 SDPL 2005Notes 5.1: Additional XPath & XSLT15 XSLT Sorting A sorted order for the processing of nodes with xsl:for-each and xls:apply-templates can be specified by A sorted order for the processing of nodes with xsl:for-each and xls:apply-templates can be specified by controlled by attributes of xsl:sort like controlled by attributes of xsl:sort like –select : expression for the sort key –data-type : "text" (default) or "number" – order : "ascending" (default) or "descending" The first xsl:sort specifies the primary sort key, the second one the secondary sort key, and so on. The first xsl:sort specifies the primary sort key, the second one the secondary sort key, and so on.

16 SDPL 2005Notes 5.1: Additional XPath & XSLT16 Example (cont; Sorted index of names) All names can be collected in a last-name-first-name order using the below template All names can be collected in a last-name-first-name order using the below template Index, Index, n This creates an UL list with items Brown, Bob Brown, Helen Brown, Helen Dobrik, Dave Brown, Bob Brown, Helen Brown, Helen Dobrik, Dave

17 SDPL 2005Notes 5.1: Additional XPath & XSLT17 What about duplicates? Is it possible to eliminate duplicate values like Brown, Helen Brown, Helen ? Is it possible to eliminate duplicate values like Brown, Helen Brown, Helen ? n Yes (but not that straightforward) n Using conditional instructions –See next

18 SDPL 2005Notes 5.1: Additional XPath & XSLT18 Conditional processing A template can be instantiated or ignored based on the value of a test Boolean expression, using A template can be instantiated or ignored based on the value of a test Boolean expression, using Template Template Example: a comma-separated list of names: Example: a comma-separated list of names:,,

19 SDPL 2005Notes 5.1: Additional XPath & XSLT19 Conditional processing (2) Also a case-like construct (  switch in Java): Also a case-like construct (  switch in Java): … … … … …

20 SDPL 2005Notes 5.1: Additional XPath & XSLT20 Example (cont; Eliminating duplicate names) No access to other nodes (except current() ) in the list of xsl:for-each No access to other nodes (except current() ) in the list of xsl:for-each –But can refer to other nodes in the source tree –Process just the first one of duplicate name s:,,

21 SDPL 2005Notes 5.1: Additional XPath & XSLT21 Generating Numbers Formatted numbers can be inserted in the result tree by element xsl:number Formatted numbers can be inserted in the result tree by element xsl:number –number can be specified by attribute value=" Expr " –otherwise the number generated by the position of the current node in the source tree n We’ll consider typical cases through examples –The complete rules of the Spec are rather complex n Example 1: Numbering list items

22 SDPL 2005Notes 5.1: Additional XPath & XSLT22 Generating numbers: Example 1 n n item itemitemolapricotbananacoconut apricotbananacoconut

23 SDPL 2005Notes 5.1: Additional XPath & XSLT23 Generating numbers: Example 2 n Hierarchical numbering (1, 1.1, 1.1.1, 1.1.2, …) for titles of chapters, titles of their sections, and titles of subsections:

24 SDPL 2005Notes 5.1: Additional XPath & XSLT24 Generating numbers: Example chapSweets title title Berries sect subsect Cherry titlechaptitle Vegetables... Sweets Sweets Berries Berries Cherry Cherry Vegetables Vegetables

25 SDPL 2005Notes 5.1: Additional XPath & XSLT25 Example 2: Variation n As above, but number titles within appendices with A, A.1, A.1.1, B.1 etc:

26 SDPL 2005Notes 5.1: Additional XPath & XSLT26 Example 2: Variation AA.1A.1.1B appendix Sweetstitletitle Berries sect subsect Cherry title titleVegetables... Sweets Sweets Berries Berries Cherry Cherry Vegetables Vegetablesappendix

27 SDPL 2005Notes 5.1: Additional XPath & XSLT27 Generating numbers: Example 3 Sequential numbering of note s within chap ters : (more precisely: starting anew at the start of any chapter) Sequential numbering of note s within chap ters : (more precisely: starting anew at the start of any chapter)

28 SDPL 2005Notes 5.1: Additional XPath & XSLT28 Ex 3: Sequential numbering from chaps chap Yes!note noteNo! sect Perhaps?note noteOK... (1)(2)(3)(1) Yes! Yes! No! No! Perhaps? Perhaps? OK OK chap


Download ppt "SDPL 2005Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support for –arithmetics –processing ID/IDREF cross-references."

Similar presentations


Ads by Google