Altova Mailing List Archives
>microsoft.public.xml Archive Home
>Thread Next - Re: XSLT - count number of elements with same name without knowing nam
XSLT - count number of elements with same name without knowing nam
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"