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

时间:2011-06-18 16:29:24

标签: 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>

现在我想知道有多少/ 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>

但结果是错误的:

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

应该是

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

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

3 个答案:

答案 0 :(得分:6)

将XPath替换为正确的XPath,即:

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

我使用substring匹配第一个属性字符代替contains,它匹配字符串中的任何字符。

答案 1 :(得分:1)

我认为这就是你需要的

count(*[contains(name(), 'm') and (@a = parent::*/*[contains(name(),'e')]/@a)])

在XSLT中使用它

<?xml version="1.0" encoding="UTF-8"?>
<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 = parent::*/*[contains(name(),'e')]/@a)])"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

产生所需的输出

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

答案 2 :(得分:0)

使用(使用当前节点任何profil元素):

count(*[starts-with(name(),'m')
      and
        @a = ../*[starts-with(name(),'e')]/@a
       ]
      )

完整的XSLT代码

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

 <xsl:template match="profil">
  <count>
   <xsl:value-of select=
   "count(*[starts-with(name(),'m')
          and
            @a = ../*[starts-with(name(),'e')]/@a
           ]
         )
   "/>
  </count>
 </xsl:template>
</xsl:stylesheet>

应用于提供的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>

生成想要的正确结果

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