xml – 如何计算具有相同属性值的元素
发布时间:2020-12-16 23:15:10 所属栏目:百科 来源:网络整理
导读:我敢肯定这是一个简单的,但我只是不看树木. 我有一个看起来像这样的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/m
|
我敢肯定这是一个简单的,但我只是不看树木.
我有一个看起来像这样的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>
有谁有人建议我做错了什么? 解决方法
用正确的XPath替换XPath,即:
<xsl:value-of select="count(*[substring(name(),1,1)='m'
and ./@a = ../*[substring(name(),1)='e']/@a])"/>
我使用substring来匹配第一个属性字符代替contains,它匹配字符串中的任何字符. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
