Xslt 检查一个节点有多少子节点
我有一个这样的XML文件:Xslt 检查一个节点有多少子节点,xslt,xpath,Xslt,Xpath,我有一个这样的XML文件: <manual> <chapter> <section /> <section /> <section /> </chapter> <chapter> <section> <section /> <section />
<manual>
<chapter>
<section />
<section />
<section />
</chapter>
<chapter>
<section>
<section />
<section />
<section />
<section />
<section />
<section />
<section />
<section />
<section />
<section />
</section>
<section />
<section />
</chapter>
</manual>
这不起作用,因为count()的输出是一个整数,而节[]需要一个节点集。像count(//section/section)这样的东西不起作用,因为这将计算所有的节,而不仅仅是一组同级
有没有办法做到这一点 我认为您需要
//section[count(section)>9]
,比较运算符不属于count
函数调用,正如您所拥有的那样。我认为您需要//section[count(section)>9]
,比较运算符不属于count
函数调用,您可以使用count(./section)+count(./section/section)查找节的总计数,下面是了解节点位置和节计数的代码
<?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" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="/manual/chapter">
<xsl:if test="count(./section) + count(./section/section) > 9">
<xsl:value-of select="position()"/>,<xsl:value-of select="count(./section) + count(./section/section) "/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
,
您可以使用count(./section)+count(./section/section)查找节的总计数,下面是了解节点位置和节计数的代码
<?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" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="/manual/chapter">
<xsl:if test="count(./section) + count(./section/section) > 9">
<xsl:value-of select="position()"/>,<xsl:value-of select="count(./section) + count(./section/section) "/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
,
它给出了的总计数。对于其他应用程序很有用,但我正在寻找“一个有9个以上子节点的节点”。这给出了节点的总数。对于其他应用程序很有用,但我正在寻找“一个有9个子节点以上的节点”。