XML/XSLT stylesheet/current group — различия между версиями
(нет различий)
|
Версия 21:22, 25 мая 2010
Содержание
Get current group
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <towns>
<town>A</town> <town>B</town> <town>C</town> <town>D</town> <town>E</town> <town>F</town> <town>G</town> <town>H</town> <town>I</town> <town>J</town> <town>K</town> <town>L</town>
</towns>
File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:param name="cols" select="4" /> <xsl:template match="towns">
<xsl:value-of select="." /> |
</xsl:for-each>
</xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
A | B | C | D |
E | F | G | H |
I | J | K | L |
</source>
Use count() and current-group() to count groups
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <employees>
<employee> <FirstName>A</FirstName> <LastName>B</LastName> <Country>USA</Country> </employee> <employee> <FirstName>C</FirstName> <LastName>D</LastName> <Country>USA</Country> </employee> <employee> <FirstName>E</FirstName> <LastName>F</LastName> <Country>USA</Country> </employee>
</employees>
File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body>
header
<xsl:for-each-group select="employees/employee" group-by="Country"> <xsl:sort select="current-grouping-key()" />
Number of employees who live in: <xsl:value-of select="current-grouping-key()" /> is <xsl:value-of select="count(current-group())" />
<xsl:result-document href="{current-grouping-key()}.xml"> <employees> <xsl:copy-of select="current-group()" /> </employees> </xsl:result-document> </xsl:for-each-group> </body> </html> </xsl:template>
</xsl:stylesheet>
</source>
Word count
<source lang="xml">
File: Data.xml <poem>
<author>author 1</author> <date>1912</date> <title>Song</title> <stanza> <line>line 1</line> <line>line 2</line> <line>line 3</line> <line>line 4</line> </stanza> <stanza> <line>line 5</line> <line>line 6</line> <line>line 7</line> <line>line 8</line> </stanza> <stanza> <line>line 9</line> <line>line 10</line> <line>line 11</line> <line>line 12</line> </stanza>
</poem> File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <wordcount> <xsl:for-each-group group-by="." select=" for $w in tokenize(string(.), "\W+") return lower-case($w)"> <xsl:sort select="count(current-group())" order="descending" /> <word word="{current-grouping-key()}" frequency="{count(current-group())}" /> </xsl:for-each-group> </wordcount> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <wordcount>
<word word="line" frequency="12"/> <word word="" frequency="2"/> <word word="1" frequency="2"/> <word word="author" frequency="1"/> <word word="1912" frequency="1"/> <word word="song" frequency="1"/> <word word="2" frequency="1"/> <word word="3" frequency="1"/> <word word="4" frequency="1"/> <word word="5" frequency="1"/> <word word="6" frequency="1"/> <word word="7" frequency="1"/> <word word="8" frequency="1"/> <word word="9" frequency="1"/> <word word="10" frequency="1"/> <word word="11" frequency="1"/> <word word="12" frequency="1"/>
</wordcount>
</source>