Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 11th, 2007, 09:58 AM
Registered User
 
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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



 
Old October 11th, 2007, 10:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old October 11th, 2007, 10:24 AM
Registered User
 
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!



 
Old October 11th, 2007, 10:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old October 11th, 2007, 10:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The forum code has kindly stripped out the ampersands from my last post, each # should be preceded by an &.

--

Joe (Microsoft MVP - XML)
 
Old October 11th, 2007, 11:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
record counting keyvanjan Classic ASP Databases 0 June 30th, 2005 01:20 AM
Counting ?!? hcweb Classic ASP Basics 2 December 8th, 2003 05:08 PM
Re-Using an Array (for strings of varying lengths) WebDevel Javascript How-To 2 December 6th, 2003 06:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.