Altova Mailing List Archives


XSLT - count number of elements with same name without knowing nam

From: jhendrich@----------.------
To: NULL
Date: 5/17/2006 7:12:00 AM

Hi,

I want to count the number of elements that have the same name and XPath 
directive.  The catch is, I don't know the XPath ahead of time.  I'm using 
XSLT 1.0.  For example, given

<colaCans>
  <coke/>
  <coke/>
  <coke/>
  <drpepper/>
</colaCans

I would want to be able to determine that there are 3 <coke> elements.  But 
I don't know the XPaths ahead of time because I'm taking in an arbitrary XML 
document (and no Schema).  I want to process all the elements of this 
arbitrary XML, but for those elements that are at the same XPath (e.g. 
multiple elements having the same name and located at the same path), I only 
want to process the first such element and skip the rest.

The XPath count function takes a node-set as its parameter.  I can't figure 
out how to generate a node-set that, given the example above, would contain 3 
<coke> elements, without knowing ahead of time to select="/colaCans/coke".   
I can of course build a string, for example, 'colaCans/coke', but as a 
string, this can't be converted to an expression, at least in XSLT 1.0/XPath 
1.0.  That is, I can't do count( /colaCans/coke ) because I don't know ahead 
of time the XPath, and I can't do count( '/colaCans/coke' ) because that's a 
string, not an expression.  If the context node is /colaCans I can't simply 
loop over each child (as in <xsl:for-each select="*">), and in the context of 
each child count( . ) because that's a singleton node set each time.

I don't think I can do what I'd like to do, but maybe someone knows a trick.
-Jon

-- 
"When in the company of others, I shall always consider myself the lowest of 
all"

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.