Presentation is loading. Please wait.

Presentation is loading. Please wait.

SDPL 2006XSLT: Additional Features and Computing1 5.1 Additional Features n XPath support for –arithmetics –processing ID/IDREF cross-references –manipulation.

Similar presentations


Presentation on theme: "SDPL 2006XSLT: Additional Features and Computing1 5.1 Additional Features n XPath support for –arithmetics –processing ID/IDREF cross-references –manipulation."— Presentation transcript:

1 SDPL 2006XSLT: Additional Features and Computing1 5.1 Additional Features 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 2006XSLT: Additional Features and Computing2 XPath: Arithmetical Operations Operators for double-precision (64 bit) floating- point numbers +, -, *, div, mod (same as % in Java) Operators for double-precision (64 bit) floating- point numbers +, -, *, div, mod (same as % in Java) Rounding numbers up, down, and to the closest integer: floor(x), ceiling(x),round(x) Rounding numbers up, down, and to the closest integer: floor(x), ceiling(x),round(x) Formatting numbers as strings (e.g.): Formatting numbers as strings (e.g.): »format-number(-1.2534, " 0.0 " ) = " -1.3 " –XSLT function; applies Java decimal format patterns

3 SDPL 2006XSLT: Additional Features and Computing3 Aggregate Functions n Counting nodes »count( node-set ) –and summing them as numbers »sum( node-set ) n Example: –Average of course grades below current node: sum(.//course/@grade) div count(.//course)

4 SDPL 2006XSLT: Additional Features and Computing4 Cross-referencing Function id selects elements by their unique ID Function id selects elements by their unique ID –NB: ID attributes must be declared in DTD (See an example later) n Examples: –id('sect:intro') selects the element with unique ID "sect:intro" –id('sect:intro')/auth[3] selects the third auth of the above element –id('sect1 sect2 sect3') selects 3 sections (with corresponding ID values)

5 SDPL 2006XSLT: Additional Features and Computing5 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' ; (NB alternative quotes) –"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 2006XSLT: Additional Features and Computing6 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 2006XSLT: Additional Features and Computing7 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 (XSLT 2.0: of all, space-separated) n Example: Transform elements like Charlie Parker Charlie Parker to the form Charlie ("Bird") Parker

8 SDPL 2006XSLT: Additional Features and Computing8 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 2006XSLT: Additional Features and Computing9 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 } Example: Transform source element Example: Transform source element<photo> Mary.jpg Mary.jpg into form into form

10 SDPL 2006XSLT: Additional Features and Computing10 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 {size/@width} are evaluated in the context of the current node (the photo element) Expressions {file} and {size/@width} are evaluated in the context of the current node (the photo element)

11 SDPL 2006XSLT: Additional Features and Computing11 XSLT: Repetition Nodes can be "pulled" from source for processing using Template Nodes can be "pulled" from source for processing using Template –Template is applied to the selected nodelist, each node in turn as the current() node »in document order, unless sorted using xsl:sort instructions (see later)

12 SDPL 2006XSLT: Additional Features and Computing12 Example (of xsl:for-each ) n Format the below document as HTML: ]> The Joy of XML Getting Started Helen Brown says that XML processing is fun. Dave Dobrik agrees. Family affairs Bob Brown is the husband of Helen Brown. Finishing Up As we discussed in, XML processing is fun. ]> The Joy of XML Getting Started Helen Brown says that XML processing is fun. Dave Dobrik agrees. Family affairs Bob Brown is the husband of Helen Brown. Finishing Up As we discussed in, XML processing is fun.

13 SDPL 2006XSLT: Additional Features and Computing13 Example: Table of contents n A table of contents can be formed of section titles: Table of Contents Table of Contents

14 SDPL 2006XSLT: Additional Features and Computing14 Example (cont; Cross references) Cross references (to sections) can also be processed using for-each: Cross references (to sections) can also be processed using 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 2006XSLT: Additional Features and Computing15 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 (default: "." ) –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 2006XSLT: Additional Features and Computing16 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 Possible to eliminate duplicates? Yes, but a bit tricky. See next

17 SDPL 2006XSLT: Additional Features and Computing17 Conditional processing n A template can be instantiated or ignored based on the value of a Boolean expression: Template Template Example: a comma-separated list of names: Example: a comma-separated list of names:,,

18 SDPL 2006XSLT: Additional Features and Computing18 An aside: Meaning of position() Evaluation wrt the current node list. Above rule applied to a source with a b c d by invocation yields" a,b,c,d " (← single node list); Evaluation wrt the current node list. Above rule applied to a source with a b c d by invocation yields" a,b,c,d " (← single node list); With invocation from it yields " a,b " and " c,d " (Clever, and tricky!) With invocation from it yields " a,b " and " c,d " (Clever, and tricky!)

19 SDPL 2006XSLT: Additional Features and Computing19 Conditional processing (2) Also a case-like construct (  switch in Java): Also a case-like construct (  switch in Java): … … … … … </xsl:choose>

20 SDPL 2006XSLT: Additional Features and Computing20 Example (cont; Eliminating duplicate names) No access to other nodes (except current() ) in the current node list No access to other nodes (except current() ) in the current node list –But can refer to nodes in the source tree –Process just the first one of duplicate name s:,, </xsl:for-each>

21 SDPL 2006XSLT: Additional Features and Computing21 Generating Numbers Formatted numbers can be inserted in the result tree by element Formatted numbers can be inserted in the result tree by element –by the position of the current node in the source tree –nodes to be counted specified by a count pattern –common numbering schemes supported: single-level, hierarchical, and sequential ignoring levels n Typical cases in following examples »(Complete specification rather complex) n Example 1: Numbering list items

22 SDPL 2006XSLT: Additional Features and Computing22 Generating numbers: Example 1 n n item itemitemolapricotbananacoconut 1.2.3. apricotbananacoconut

23 SDPL 2006XSLT: Additional Features and Computing23 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 2006XSLT: Additional Features and Computing24 Generating numbers: Example 2 11.11.1.12 chapSweets title title Berries sect subsect Cherry titlechaptitle Vegetables... Sweets Sweets Berries Berries Cherry Cherry Vegetables Vegetables

25 SDPL 2006XSLT: Additional Features and Computing25 Example 2: Variation n As above, but number titles within appendices with A, A.1, A.1.1, B.1 etc:

26 SDPL 2006XSLT: Additional Features and Computing26 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 2006XSLT: Additional Features and Computing27 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 2006XSLT: Additional Features and Computing28 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

29 SDPL 2006XSLT: Additional Features and Computing29 5.2 Computing with XSLT n XSLT is a declarative rule-based language –for a special purpose: XML transformations –Could we use XSLT for procedural computing? –What is the exact computational power of XSLT? n We've seen some programming-like features: –iteration over source nodes ( xsl:for-each ) –conditional evaluation ( xsl:if and xsl:choose )

30 SDPL 2006XSLT: Additional Features and Computing30 Computing with XSLT n Further programming-like features: –variables (names bound to non-updatable values):...... –variables (names bound to non-updatable values):...... –callable named templates with parameters: –callable named templates with parameters:

31 SDPL 2006XSLT: Additional Features and Computing31 Visibility of Variable Bindings The binding is visible in following siblings of xsl:variable, and in their descendants: The binding is visible in following siblings of xsl:variable, and in their descendants:..................

32 SDPL 2006XSLT: Additional Features and Computing32 A Real-Life Example We used LaTeX to format an XML article. For this, we needed to map source table structures... to corresponding LaTeX environments: \begin{tabular}{lll} %3 left-justified cols... \end{tabular} We used LaTeX to format an XML article. For this, we needed to map source table structures... to corresponding LaTeX environments: \begin{tabular}{lll} %3 left-justified cols... \end{tabular} n How to do this?

33 SDPL 2006XSLT: Additional Features and Computing33 Possible solution (for up to 4 columns) \begin{tabular}{l } \begin{tabular}{l } \end{tabular} \end{tabular}</xsl:template>  OK, but inelegant  How to support arbitrarily many columns? 1">l 1">l</xsl:if > 2">l 2">l</xsl:if > 3">l > 3">l

34 SDPL 2006XSLT: Additional Features and Computing34 More General Solution (1/2) Pass the column-count to a named template which generates the requested number of ‘ l ’s: Pass the column-count to a named template which generates the requested number of ‘ l ’s: \begin{tabular}{ } \begin{tabular}{ } \end{tabular} \end{tabular}

35 SDPL 2006XSLT: Additional Features and Computing35 Solution 2/2: Recursive gen-cols 0"> 0"> </xsl:template> formal parameters

36 SDPL 2006XSLT: Additional Features and Computing36 Stylesheet Parameters Stylesheet can get params from command line, or through JAXP with Transformer.setParameter(name, value) : Stylesheet can get params from command line, or through JAXP with Transformer.setParameter(name, value) : </xsl:transform> $ java -jar saxon.jar dummy.xml double.xslt In=120 240

37 SDPL 2006XSLT: Additional Features and Computing37 Computational power of XSLT n XSLT seems quite powerful, but how powerful is it? –Implementations provide extension mechanisms, e.g., to call arbitrary Java methods –Are there limits to XSLT processing that we can do without extensions? n Any algorithm can be shown computable with plain XSLT –by simulating Turing machines, by a recursive named template with string parameters (see 2005 lecture notes, or References)

38 SDPL 2006XSLT: Additional Features and Computing38 What does this mean? n XSLT has full algorithmic power –(It is "Turing-complete") –Is this intentional? »Inconvenient as a general-purpose programming language! –Impossible to recognise non-terminating transformations automatically (  the "halting problem" has no algorithmic solution) »could attempt "denial-of-service" attacks with non-terminating style sheets


Download ppt "SDPL 2006XSLT: Additional Features and Computing1 5.1 Additional Features n XPath support for –arithmetics –processing ID/IDREF cross-references –manipulation."

Similar presentations


Ads by Google