如何检查元素的直接节点是文本?

时间:2016-03-28 12:36:20

标签: xml xslt xslt-2.0

建议如何检查元素'simplepara'后面是否有文字。 [模板匹配应来自下面的XSLT中给出的'simplepara']

XML:

<article>
<simplepara>Fig 1</simplepara>The text1<simplepara>Fig 2</simplepara><simplepara>Fig 3</simplepara>The text2<simplepara>Fig 4</simplepara><simplepara>Fig 5</simplepara> the text3<simplepara>Fig 5</simplepara>
</article>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="simplepara">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
    <xsl:if test="following-sibling::node()[1][text()][normalize-space()!='']">
        <xsl:comment select="'Text-Node'"/>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

必填结果:

<?xml version="1.0" encoding="UTF-8"?><article>
<simplepara>Fig 1</simplepara><!--Text-Node-->The text1<simplepara>Fig 2</simplepara><simplepara>Fig 3</simplepara><!--Text-Node-->The text2<simplepara>Fig 4</simplepara><simplepara>Fig 5</simplepara><!--Text-Node--> the text3<simplepara>Fig 5</simplepara>
</article>

1 个答案:

答案 0 :(得分:3)

更改xpath检查以使用self轴而不是子轴:

following-sibling::node()[1][self::text()][normalize-space()!='']