Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count elements with same attribute value

Tags:

xml

xslt

xpath

I'm sure this is an easy one but i'm just not to see the wood for the trees.

I'm having an XML that look like this:

   <root>
    <profil>
        <e1 a="2">1</e1>
        <m1 a="3">1</m1>
        <e2 a="4">1</e2>
        <m2 a="5">1</m2>
    </profil>
    <profil>
        <e1 a="5">1</e1>
        <m1 a="3">1</m1>
        <e2 a="4">1</e2>
        <m2 a="4">1</m2>
    </profil>
    <profil>
        <e1 a="7">1</e1>
        <m1 a="7">1</m1>
        <e2 a="4">1</e2>
        <m2 a="2">1</m2>
    </profil>
</root>

Now I want to know how many /m*/@a are equal to e*/@a per /profil. So I came up with the following XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*">
        <xsl:element name="root">
            <xsl:for-each select="/root/profil">
                <xsl:element name="count">
                    <xsl:value-of select="count(*[contains(name(), 'm') and ./@a = //*[contains(name(),'e')]/@a])"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

But the result is wrong:

<root>
    <count>1</count>
    <count>1</count>
    <count>2</count>
</root>

It should be

<root>
    <count>0</count>
    <count>1</count>
    <count>1</count>
</root>

Does anyone has a suggestion what I'm doing wrong?

like image 418
Sdudda Avatar asked Jun 18 '11 16:06

Sdudda


1 Answers

Replace the XPath with the correct one, which is:

<xsl:value-of select="count(*[substring(name(),1,1)='m' 
      and ./@a = ../*[substring(name(),1,1)='e']/@a])"/>

I've used substring to match the first attribute character in place of contains which matches any character in a string.

like image 108
Emiliano Poggi Avatar answered Sep 18 '22 07:09

Emiliano Poggi