我有一个样本”:
<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>
然后我应用一个如下所示的模板:
<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>
但我希望输出看起来像这样,如果 A 后面没有兄弟,我们就忽略这个。只需打印这些 @rename 包含“选项”并且还包含元素
option1: 1
option2: 3
option4: 4
我现在得到的是:
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