edwin_uestc
edwin_uestc

Reputation: 189

xslt count sibling

I have a sample":

<Root>
    <A rename="yes,it is option 1"/>
    <C rename="no"/>
    <A rename="yes,it is option 2"/>
    <C rename="no"/>
    <C rename="yes"/>
    <C rename="no"/>  
    <A rename="yes,it is option 3"/>
    <A rename="yes,it is option 4"/>
    <C rename="no"/>
    <C rename="yes"/>
    <C rename="no"/>      
    <C rename="no"/>        
</Root>

Then I apply a template look like this:

   <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="A">
            <p><b>option1: <xsl:value-of select="count(following-sibling::C[preceding-sibling::A[1]/@rename[contains(.,'option 1')]])"/></b></p>
            <p><b>option2: <xsl:value-of select="count(following-sibling::C[preceding-sibling::A[1]/@rename[contains(.,'option 2')]])"/></b></p>            
            <p><b>option3: <xsl:value-of select="count(following-sibling::C[preceding-sibling::A[1]/@rename[contains(.,'option 3')]])"/></b></p>
            <p><b>option4: <xsl:value-of select="count(following-sibling::C[preceding-sibling::A[1]/@rename[contains(.,'option 4')]])"/></b></p>                        
 </xsl:template>
</xsl:stylesheet>

but i want the output look like this,if there are not sibling following A, we just ignore this one.just print these @rename contains "option" and also contains elements

option1: 1 

option2: 3 

option4: 4 

What I get now is:

option1: 1 

option2: 3 

option3: 0 

option4: 4 

option1: 0 

option2: 3 

option3: 0 

option4: 4 

option1: 0 

option2: 0 

option3: 0 

option4: 4 

option1: 0 

option2: 0 

option3: 0 

option4: 4 

Upvotes: 1

Views: 3514

Answers (2)

samjudson
samjudson

Reputation: 56893

In XSLT 2.0 this turns into an easy grouping issue, using the xsl:for-each-group instruction.

<xsl:template match="Root">
  <xsl:for-each-group group-starting-with="A" select="*">
    <xsl:if test="count(current-group()) != 1">
      <p>Option <xsl:value-of select="substring-after(current-group()[1]/@rename, 'option ')"/> = <xsl:value-of select="count(current-group())-1"/></p>        </xsl:if>
  </xsl:for-each-group>
</xsl:template>

Upvotes: 0

Tim C
Tim C

Reputation: 70648

You are counting all the C elements that follow the most recent A element. This is can be done by means of a key, to group all such C elements with the associated A element

<xsl:key name="lookup" match="C" use="generate-id(preceding-sibling::A[1])" />

To find A elements for which at least one following C exists, you then do this:

<xsl:apply-templates select="A[key('lookup', generate-id())]" />

And then to count the number of options, you can just count the number of elements in the key, like so:

<xsl:value-of select="count(key('lookup', generate-id()))" />

Try the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>
    <xsl:key name="lookup" match="C" use="generate-id(preceding-sibling::A[1])" />

    <xsl:template match="/Root">
        <xsl:apply-templates select="A[key('lookup', generate-id())]" />
    </xsl:template>

    <xsl:template match="A">
        <xsl:value-of select="concat(@rename, ': ', count(key('lookup', generate-id())) , '&#13;&#13;')" />
    </xsl:template>
</xsl:stylesheet> 

When this is applied to you XML sample, the following is output:

yes,it is option 1: 1

yes,it is option 2: 3

yes,it is option 4: 4

Note that if you wanted to 'option1' instead of 'yes, it is option 1', you could do concat('option', substring-after(@rename, 'option ')) instead of just @rename

Note, this issue could be simplified, if you re-structured your XML. Something like this would be much better:

<Root>
    <A rename="yes,it is option 1">
        <C rename="no"/>
    </A>
    <A rename="yes,it is option 2">
        <C rename="no"/>
        <C rename="yes"/>
        <C rename="no"/>
    </A>
    <A rename="yes,it is option 3"/>
    <A rename="yes,it is option 4">
        <C rename="no"/>
        <C rename="yes"/>
        <C rename="no"/>
        <C rename="no"/>
    </A>
</Root>

Upvotes: 1

Related Questions