You can match all h5
and only copy / do whatever with the nodes, if they meet a certain condition. This condition is to check the last text node (or [2]
for second), whether it is equal to some hardcoded string. You may need to use normalize-space()
to eleminate whitespace from the text node:
<?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:template match="/">
<result>
<xsl:apply-templates select="root/h5"/>
</result>
</xsl:template>
<xsl:template match="h5">
<xsl:if test="normalize-space(./text()[last()]) = 'C'">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Result:
<?xml version="1.0" encoding="utf-8"?>
<result>
<h5>
A
<span>xxx</span>
C
</h5>
</result>