XPT 2006 XSLT: Additional Features and Computing 1
3.1 Additional Features 3.1 Additional Features
- XPath
XPath support for support for
– – arithmetics arithmetics – – processing ID/IDREF cross processing ID/IDREF cross-
- references
references – – manipulation of strings manipulation of strings
- Generating text
Generating text
– – for content for content – – for attribute values for attribute values
- Repetition, sorting and conditional processing
Repetition, sorting and conditional processing
- Generating numbers
Generating numbers
XPT 2006 XSLT: Additional Features and Computing 2
XPath XPath: Arithmetical Operations : Arithmetical Operations
- Operators for double
Operators for double-
- precision (64 bit) floating
precision (64 bit) floating-
- point numbers
point numbers
+, +, -
- , *, div, mod
, *, div, mod (same as (same as % % in Java) in Java)
- Rounding numbers up, down, and to the closest integer:
Rounding numbers up, down, and to the closest integer: floor(x floor(x), ), ceiling(x),round(x ceiling(x),round(x) )
- Formatting numbers as strings (e.g.):
Formatting numbers as strings (e.g.):
» » format format-
- number(
number(-
- 1.2534,
1.2534, "
"0.0
0.0"
") =
) = "
"-
- 1.3
1.3"
"
– – XSLT XSLT function; applies Java decimal format patterns function; applies Java decimal format patterns
XPT 2006 XSLT: Additional Features and Computing 3
Aggregate Functions Aggregate Functions
- Counting nodes
Counting nodes
» » count( count(node node-
- set
set) )
– – and summing them as numbers and summing them as numbers
» » sum( sum(node node-
- set
set) )
- Example:
Example:
– – Average of course grades below current node: Average of course grades below current node: sum( sum(.//course/@grade .//course/@grade) ) div div count( count(.//course .//course) )
XPT 2006 XSLT: Additional Features and Computing 4
Cross Cross-
- referencing
referencing
- Function
Function id id selects elements by their unique ID selects elements by their unique ID
– – NB NB: ID attributes must be declared in DTD : ID attributes must be declared in DTD (See an example later) (See an example later)
- Examples:
Examples:
– – id('sect:intro') id('sect:intro') selects the element with unique ID selects the element with unique ID "sect:intro" "sect:intro" – – id('sect:intro')/para[5] id('sect:intro')/para[5] selects the fifth selects the fifth para para child of the above element child of the above element – – id('sect1 sect2 sect3') id('sect1 sect2 sect3')selects 3 sections selects 3 sections (with corresponding ID values) (with corresponding ID values)
XPT 2006 XSLT: Additional Features and Computing 5
String manipulation String manipulation
- Equality and inequality of strings can be tested
Equality and inequality of strings can be tested with operators with operators = = and and != !=
– – " "foo foo" = ' " = 'foo foo' '; ; (NB alternative quotes) (NB alternative quotes) – – " "foo foo" != " " != "Foo Foo" "
- Testing for substrings:
Testing for substrings:
– – starts starts-
- with("
with("dog dogbert bert", " ", "dog dog") = true() ") = true() – – contains("do contains("dogbe gbert rt", " ", "gbe gbe") = true() ") = true()
- Concatenation (of two or more strings),
Concatenation (of two or more strings),
– – concat("dog concat("dog", " ", "bert bert") = " ") = "dogbert dogbert" "
XPT 2006 XSLT: Additional Features and Computing 6
XPath XPath: more string functions : more string functions
– – substring substring-
- before("ftp
before("ftp: :// //a", " a", "// //") = ") = substring substring-
- before("ftp
before("ftp: :/ //a", " /a", "/ /") = "ftp:" ") = "ftp:"
– – substring substring-
- after("ftp
after("ftp: :/ //a", " /a", "/ /")= "/a" ")= "/a" – – substring( substring(string string, start, length? , start, length?): ):
» » substring("dogbert substring("dogbert", 1, 3) = "dog" ", 1, 3) = "dog" » » substring("dogbert substring("dogbert", 3) = " ", 3) = "gbert gbert" "
– – string string-
- length("dogbert
length("dogbert")=7 ")=7 – – translate( translate(Str Str, Replaced, Replacing , Replaced, Replacing): ):
» » translate(" translate("dogg doggy"," y","dgo dgo","Ssi ","Ssi")="Sissy" ")="Sissy"
XPT 2006 XSLT: Additional Features and Computing 7
Generating Text Generating Text
- The string
The string-
- value of an expression can be inserted in the
value of an expression can be inserted in the result tree by instruction result tree by instruction < <xsl:value xsl:value-
- of
- f select="
select="Expr Expr" /> " />
– – if if Expr
Expr evaluates to a node
evaluates to a node-
- set,
set, value of the first node value of the first node in in document order is used document order is used
- Consider transforming source elements like
Consider transforming source elements like
<name alias="Bird"> <name alias="Bird"> <first>Charlie</first><last>Parker</last> <first>Charlie</first><last>Parker</last> </name> </name>
to the form to the form
Charlie ("Bird") Parker Charlie ("Bird") Parker
XPT 2006 XSLT: Additional Features and Computing 8
Computing generated text (2) Computing generated text (2)
- This can be specified by template rule
This can be specified by template rule
< <xsl:template xsl:template match="name"> match="name"> < <xsl:value xsl:value-
- of select="first" />
- f select="first" />
("< ("<xsl:value xsl:value-
- of select="@alias" />")
- f select="@alias" />")
< <xsl:value xsl:value-
- of select="last" />
- f select="last" />
< <xsl:text xsl:text> > </ </xsl:text xsl:text> > </ </xsl:template xsl:template> >
- Verbatim text (like the white
Verbatim text (like the white-
- space above) can be