count tags

A

adMjb

Hi, I want to count the tags within another tag, I have been trying the
count children, count this, count that, but had no luck, can anyone
help please. i.e:

XML
===========
<root>
<person>
<name>Adam</name>
<name>John</name>
<name>Mark</name>
<age>10</age>
</person>
<person>
<name>Jim</name>
<name>Jill</name>
</person>
</root>
=============
what I want is to count the name tag within person and desplay with an
attribute name="?" i.e:
=============
<root>
<person name="3">
<name>Adam</name>
<name>John</name>
<name>Mark</name>
<age>10</age>
</person>
<person name="2">
<name>Jim</name>
<name>Jill</name>
</person>
</root>
=============
 
G

George Bina

Hi,

Start with a recursive copy template and add a specific rule for the
person element that sopies that along with copying that element to the
output adds also the name attribute:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="person">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="name"><xsl:value-of
select="count(name)"/></xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Best Regards,
George
 
A

adMjb

Hi George, and thanks.

One more, what if I wanted to add age number to that i.e?

<person name="3" age="?">

Sorry I should have said.

Kind Regards,


Adam
 
G

George Bina

Hi Adam,

Just add another attribute named age after the name attribute and set
its value to whatever you need (count of age elements, the value of the
age element, ?, etc.)

Best Regards,
George
 
P

Peter Flynn

adMjb said:
Hi, I want to count the tags within another tag,

I think you want to count elements, not tags.
Each element normally has two tags (start-tag and end-tag).

I have been trying the
count children, count this, count that, but had no luck, can anyone
help please. i.e:

XML
===========
<root>
<person>
<name>Adam</name>
<name>John</name>
<name>Mark</name>
<age>10</age>
</person>
<person>
<name>Jim</name>
<name>Jill</name>
</person>
</root>
=============
what I want is to count the name tag within person and desplay with an
attribute name="?" i.e:
=============
<root>
<person name="3">
<name>Adam</name>
<name>John</name>
<name>Mark</name>
<age>10</age>
</person>
<person name="2">
<name>Jim</name>
<name>Jill</name>
</person>
</root>

<xsl:template match="person">
<person name="{count(name)}" age="{age}">
...whatever...
</person>
</xsl:template>

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,859
Latest member
VallieMcKe

Latest Threads

Top