如何计算具有相同属性值的元素

Sdu*_*dda 5 xml xslt xpath

我敢肯定这是一个简单的,但我只是不看树木.

我有一个看起来像这样的XML:

   <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>
Run Code Online (Sandbox Code Playgroud)

现在我想知道有多少/ m*/@ a等于e*/@ a per/profil.所以我提出了以下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>
Run Code Online (Sandbox Code Playgroud)

但结果是错误的:

<root>
    <count>1</count>
    <count>1</count>
    <count>2</count>
</root>
Run Code Online (Sandbox Code Playgroud)

它应该是

<root>
    <count>0</count>
    <count>1</count>
    <count>1</count>
</root>
Run Code Online (Sandbox Code Playgroud)

有谁有人建议我做错了什么?

Emi*_*ggi 6

用正确的XPath替换XPath,即:

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

我曾经习惯substring匹配第一个属性字符代替contains字符串中的任何字符.