該轉化:
<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>
想要的,正確的結果是親duced:
<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>
謝謝,我編輯的問題 – user2204573 2013-03-24 18:19:21
@ user2204573,請參閱更新這個答案。 – 2013-03-24 18:55:42
再次感謝。 LINK元素不應該被包裝在STYLE元素之間。 PARAGRAPH元素在PARAGRAPH和BOLD元素的內容中應該有STYLE元素,但NOT LINK元素 – user2204573 2013-03-25 01:20:09