[xsl] Re: counting again

Subject: [xsl] Re: counting again
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 12 Jul 2001 07:05:13 -0700 (PDT)
Hi Michael,

1. Just change:

> 			<xsl:value-of
select="count(preceding-sibling::*[name()=$name])" />

to:

                <xsl:value-of select="count(preceding::*[name()=$name])" />

2. at a suitable place add:

        <countings>
            <EL1_LNR><xsl:value-of select="count(/ROOT/*/SUBTABLE/EL1)"/></EL1_LNR>
            <EL2_LNR><xsl:value-of select="count(/ROOT/*/SUBTABLE/EL2)"/></EL2_LNR>
        </countings>

The complete stylesheet that produces the results you want is listed below -- I just
introduced the above two changes to your original code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="/">
        <xsl:apply-templates />
        <countings>
            <EL1_LNR><xsl:value-of select="count(/ROOT/*/SUBTABLE/EL1)"/></EL1_LNR>
            <EL2_LNR><xsl:value-of select="count(/ROOT/*/SUBTABLE/EL2)"/></EL2_LNR>
        </countings>
    </xsl:template>

    <xsl:template match="EL1 | EL2">
        <xsl:variable name="name" select="name()" />

        <xsl:copy>
            <xsl:element name="{$name}_NR">
                <xsl:value-of select="count(preceding::*[name()=$name])" />
            </xsl:element>

            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Hope this helped.

Cheers,
Dimitre Novatchev.


Michael Schäfer wrote:

Hello,

I have the following XML-doc
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test3.xsl"?>
<ROOT>
	<TABLE1>
		<SUBTABLE>
			<EL1>A</EL1>
			<EL1>B</EL1>
			<EL1>C</EL1>
			<EL2>A</EL2>
			<EL2>B</EL2>
			<EL2>C</EL2>
		</SUBTABLE>
	</TABLE1>
	<TABLE2>
		<SUBTABLE>
			<EL1>D</EL1>
			<EL1>E</EL1>
			<EL2>D</EL2>
			<EL2>E</EL2>
			<EL2>F</EL2>
			<EL2>G</EL2>
		</SUBTABLE>
	</TABLE2>
</ROOT>

I want to add a number to every EL1 and EL2-Element. So I added the
template
<xsl:template match="EL1 | EL2">
	<xsl:variable name="name" select="name()"/>
	<xsl:copy>
		<xsl:element name="{$name}_NR">
			<xsl:value-of
select="count(preceding-sibling::*[name()=$name])" />
		</xsl:element>
		<xsl:apply-templates select="node()|@*"/>
	</xsl:copy>
</xsl:template>

to the xsl-file.

The result is
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test3.xsl"?>
<ROOT>
	<TABLE1>
		<SUBTABLE>
			<EL1>
				<EL1_NR>0</EL1_NR>A</EL1>
			<EL1>
				<EL1_NR>1</EL1_NR>B</EL1>
			<EL1>
				<EL1_NR>2</EL1_NR>C</EL1>
			<EL2>
				<EL2_NR>0</EL2_NR>A</EL2>
			<EL2>
				<EL2_NR>1</EL2_NR>B</EL2>
			<EL2>
				<EL2_NR>2</EL2_NR>C</EL2>
		</SUBTABLE>
	</TABLE1>
	<TABLE2>
		<SUBTABLE>
			<EL1>
				<EL1_NR>0</EL1_NR>D</EL1>
			<EL1>
				<EL1_NR>1</EL1_NR>E</EL1>
			<EL2>
				<EL2_NR>0</EL2_NR>D</EL2>
			<EL2>
				<EL2_NR>1</EL2_NR>E</EL2>
			<EL2>
				<EL2_NR>2</EL2_NR>F</EL2>
			<EL2>
				<EL2_NR>3</EL2_NR>G</EL2>
		</SUBTABLE>
	</TABLE2>
</ROOT>

What I wanted to get is
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test3.xsl"?>
<ROOT>
	<TABLE1>
		<SUBTABLE>
			<EL1>
				<EL1_NR>0</EL1_NR>A</EL1>
			<EL1>
				<EL1_NR>1</EL1_NR>B</EL1>
			<EL1>
				<EL1_NR>2</EL1_NR>C</EL1>
			<EL2>
				<EL2_NR>0</EL2_NR>A</EL2>
			<EL2>
				<EL2_NR>1</EL2_NR>B</EL2>
			<EL2>
				<EL2_NR>2</EL2_NR>C</EL2>
		</SUBTABLE>
	</TABLE1>
	<TABLE2>
		<SUBTABLE>
			<EL1>
				<EL1_NR>3</EL1_NR>D</EL1>
			<EL1>
				<EL1_NR>4</EL1_NR>E</EL1>
			<EL2>
				<EL2_NR>3</EL2_NR>D</EL2>
			<EL2>
				<EL2_NR>4</EL2_NR>E</EL2>
			<EL2>
				<EL2_NR>5</EL2_NR>F</EL2>
			<EL2>
				<EL2_NR>6</EL2_NR>G</EL2>
		</SUBTABLE>
	</TABLE2>
</ROOT>

I tried count(//preceding-sibling::*[name()=$name]) but then the result
is 12 every where. Some ideas?

And additionally I want to add a entity to the doc with the last
countings
<COUNTINGS>
	<EL1_LNR>5</EL1_LNR>
	<EL2_LNR>7</EL2_LNR>
</COUNTINGS>


Mit freundlichen Grüßen
Michael Schäfer




__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread