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