[thelist] RE: XSL - Count child nodes

Timothy Kuhn (CW) tkuhn-cw at starbucks.com
Thu Jan 27 10:56:28 CST 2005


Peter, I'm sure you can do what you want with your xml structured the
way it is. The node <VALUE>Value 1.2</VALUE> is a child of <SPEC> and
would output 
2 Value 1.2 if you are doing a for-each of the SPEC node. One suggestion
would be to change the xml if that is possible. I worked this out real
quick so it may be buggy. This xml 
<test>
	<SPEC>
		<NAME>Item 1</NAME>
		<VALUE>Value 1</VALUE>
		<SUBVALUE>
			<VALUE>Value 1.2</VALUE>
		</SUBVALUE>
	</SPEC>
	<SPEC>
		<NAME>Item 2</NAME>
		<VALUE>Value 2</VALUE>
	</SPEC>
	<SPEC>
		<NAME>Item 3</NAME>
		<VALUE>Value 3</VALUE>
	</SPEC>
</test>
With this xslt
<xsl:template match="test">
		<html>
			<head>
				<title>Sample XSLT Stylesheet</title>
			</head>
			<body>
				<xsl:for-each select="SPEC">
					<xsl:value-of
select="position()"/>
					<xsl:value-of select="NAME"/>
					<br/>
					<xsl:value-of
select="position()"/>
					<xsl:value-of select="VALUE"/>
					<br/>
					<xsl:for-each select="SUBVALUE">
						<xsl:value-of
select="position()"/>
						<xsl:value-of
select="VALUE"/>
						<br/>
					</xsl:for-each>
					<br/>
				</xsl:for-each>
			</body>
		</html>
	</xsl:template>
Outputs this
1Item 1
1Value 1
1Value 1.2

2Item 2
2Value 2

3Item 3
3Value 3
I hope that may give you something to work with.

-----Original Message-----
From: "Peter Dirickson" <peterdirickson at hotmail.com>
Hi,
Can someone please help me. I have these XML below:
<SPEC>
            <NAME>Item 1</NAME>
            <VALUE>Value 1</VALUE>
</SPEC>
<SPEC>
            <VALUE>Value 1.2</VALUE>
</SPEC>
<SPEC>
            <NAME>Item 2</NAME>
            <VALUE>Value 2</VALUE>
</SPEC>
<SPEC>
            <NAME>Item 3</NAME>
            <VALUE>Value 3</VALUE>
</SPEC>
And I would like to output this:
1. Item 1
1. Value 1
1. Value 1.2
2. Item 2
2. Value 2
3. Item 3
3. Value 3


More information about the thelist mailing list