 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 11th, 2007, 09:58 AM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
counting attribute lengths
I'm sure there is an easy way to do this that I haven't stumbled upon yet. I have xml that at its most basic looks something like this:
<data>
<row><id>1234</id></row>
<row><id>5678</id></row>
<row><id>223344</id></row>
<row><id>557799</id></row>
<row><id>112244</id></row>
<row><id>888999000</id></row>
<row><id>111222333</id></row>
</data>
thanx in advance for any insight
Note how the length of the id node can be of 4, 6 or 9 characters. I'm trying to write a snippet of xsl that will allow me to assign the total counts for each length to 3 different variables, so that for this example I would have:
$4count = 2
$6count = 3
$9count = 2
|
|

October 11th, 2007, 10:12 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You need the string-length and count functions, there are quite a few ways of doing this though, here's one:
Code:
<xsl:variable name="id-4-count" select="count(/*/row/id[string-length(.) = 4])"/>
--
Joe ( Microsoft MVP - XML)
|
|

October 11th, 2007, 10:24 AM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Joe,
well I was at least close because I was playing around with both the count and the string-length functions, I just couldn't find a way to combine them that was working for me. Your solution works perfectly though, thank you for your help!
|
|

October 11th, 2007, 10:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
The more sophisticated way would be to set up a key:
Code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="id-by-length" match="id" use="string-length(.)"/>
<xsl:template match="/">
<xsl:text>id length 4: </xsl:text><xsl:value-of select="count(key('id-by-length', 4))"/>
<xsl:text>#x0a;</xsl:text>
<xsl:text>id length 6: </xsl:text>
<xsl:value-of select="count(key('id-by-length', 6))"/>
<xsl:text>#x0a;</xsl:text>
<xsl:text>id length 8: </xsl:text>
<xsl:value-of select="count(key('id-by-length', 8))"/>
</xsl:template>
</xsl:stylesheet>
--
Joe ( Microsoft MVP - XML)
|
|

October 11th, 2007, 10:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
The forum code has kindly stripped out the ampersands from my last post, each # should be preceded by an &.
--
Joe ( Microsoft MVP - XML)
|
|

October 11th, 2007, 11:29 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
In suggesting the use of grouping, of course, I was assuming that you might have "attributes" of any length, not known in advance. If you know the only lengths will be 4, 6 and 8 then it's much easier.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |