这种转变
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[(preceding::s1 | preceding::s2)[last()][self::s1]]">
<span class="spanClass" id="{generate-id()}">
<xsl:copy-of select="."/>
</span>
</xsl:template>
<xsl:template match="s1|s2"/>
</xsl:stylesheet>
当应用于原始XML文档时(更正为格式良好):
<a>
<b>Some <s1 id="1" />text here</b>
<c>Some <s2 id="1"/>more text <s1 id="2"/> here</c>
<d>More data</d>
<e>Some <s2 id="2" />more data</e>
</a>
产生所需的输出:
<a>
<b>Some <span class="spanClass" id="IDANI2QB">text here</span></b><span class="spanClass" id="IDAOI2QB">
</span><c><span class="spanClass" id="IDAQI2QB">Some </span>more text <span class="spanClass" id="IDAWI2QB"> here</span></c><span class="spanClass" id="IDAXI2QB">
</span><d><span class="spanClass" id="IDAYI2QBIDAYI2QB">More data</span></d><span class="spanClass" id="IDAZI2QB">
</span><e><span class="spanClass" id="IDA1I2QB">Some </span>more data</e>
</a>