Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath- Count XML Tag Occurences

Status
Not open for further replies.

flanakin

Programmer
Mar 21, 2000
158
US
I need a way to count the number of times a tag occurs. Normally, this would be easy: count(tag). Problem is, I cannot specify the tag name, it has to be figured dynamically.

<xsl:value-of select=&quot;count(../*[name(../*)=name(.)])&quot; />

That is what I have now, but it doesn't work. The name(../*) only returns the first tag's name. I need to find out if the current tag is unique or not, that's all. Any help is GREATLY appreciated. Thanks in advance!!! ________________________________________
Michael C Flanakin
Indigo Web Systems
michael.flanakin@indigows.com
 
try something like

count(preceding-sibling::*[name() = name(.)]

if not 0 it can't be unique. this is inefficient as you'll be revisiting the same nodes a lot.

../* says return all nodes of my parent, ie my siblings; otherwise you might want preceding::*.

there is a much more effiecient, and somewhat more complex method; search for 'xml duplicates'
 
I'm not worried about efficiency. I am more worried about getting it done. I can revisit it for efficiency.

Here's what I have:

<xsl:value-of select=&quot;count(preceding-sibling::*[name() = name(.)]&quot; />

This gives me a count of all of the preceding children. I just need to know if the tag is unique or not. Not just checking preceding children, but ALL children. ________________________________________
Michael C Flanakin
Indigo Web Systems
michael.flanakin@indigows.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top