这种转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template>
<xsl:template match="bold[link]/text()">
<bold><xsl:value-of select="."/></bold>
</xsl:template>
</xsl:stylesheet>
当应用于提供的 XML 文档(提供的片段包装到单个顶部元素中)时:
<t>
<paragraph>
This is some text that is <bold>correct way</bold> <link>need
to be linked </link> to a document
</paragraph>
<paragraph>
This is some text that is <bold>incorrect <link>need
to be linked </link> way </bold> to a document
</paragraph>
</t>
产生想要的正确结果:
<t>
<paragraph>
This is some text that is <bold>correct way</bold>
<link>need
to be linked </link> to a document
</paragraph>
<paragraph>
This is some text that is <bold>incorrect </bold>
<link>need
to be linked </link>
<bold> way </bold> to a document
</paragraph>
</t>
OP 更新了问题 - 这是实现初始 + 新要求的稍作修改的转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="paragraph[bold]">
<paragraph>
<style name="bold"><xsl:apply-templates/></style>
</paragraph>
</xsl:template>
<xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template>
<xsl:template match="bold[link]/text()">
<bold><xsl:value-of select="."/></bold>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下文档时:
<t>
<paragraph>
This is some text that has no style
</paragraph>
<paragraph>
This is some text that is <bold>correct way</bold> <link>need
to be linked </link> to a document
</paragraph>
<paragraph>
This is some text that is <bold>incorrect <link>need
to be linked </link> way </bold> to a document
</paragraph>
</t>
产生了想要的正确结果:
<t>
<paragraph>
This is some text that has no style
</paragraph>
<paragraph>
<style name="bold">
This is some text that is <bold>correct way</bold>
<link>need
to be linked </link> to a document
</style>
</paragraph>
<paragraph>
<style name="bold">
This is some text that is <bold>incorrect </bold>
<link>need
to be linked </link>
<bold> way </bold> to a document
</style>
</paragraph>
</t>