这里是一个真正 XSLT 2.0解决方案,涵盖了最复杂的情况下,未涉及其他答案:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[matches(name(), '^title.*?\d+$')]">
<xsl:element name="{replace(name(), '^(title.*?)\d+$', '$1')}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
当下面的XML文档应用这种转变:
<t>
<title1>some title</title1>
<some_other_nodes>....</some_other_nodes>
<title2>some title2</title2> . . .
<title5>....</title5>
<titleX253>Complex title</titleX253>
</t>
的想要生成正确的结果:
<t>
<title>some title</title>
<some_other_nodes>....</some_other_nodes>
<title>some title2</title> . . .
<title>....</title>
<titleX>Complex title</titleX>
</t>
待办事项元素:
<titleX253>Complex title</titleX253>
应该转化为:
<titleX>Complex title</titleX>
这不是由Sean Durkin的答案呢!
您使用XSLT1.0还是XSLT2.0?另外,这个数字是否总是在最后,或者你能得到中间是数字的元素,比如'title1extra'? –
XSLT2.0和数字总是在最后。 – Juicef
@Juicef,现在您已经拥有了一个真正的XSLT 2.0解决方案,即使在最复杂的情况下也能正常工作 - 目前被接受的答案无法产生想要的结果。 –