末父标签
问题描述:
我试图搞清楚XSL如何结束元素<大胆>它元素的父末父标签
编辑此外,元素<款>和<大胆>的内容得到了<风格名称 =“粗体”>。排除内容<链接>元素。 <款式>将包装在<段落的内容>和<加粗>。此外元素<款>的内容可以有一个或多个<大胆>和<链接>
输入XML
<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>
输出XML应该
<paragraph>
This is some text that has no style
</paragraph>
<paragraph>
<style name="bold">This is some text that is <bold>correct way</bold></style>
<link>need to be linked </link>
<style name="bold"> to a document</style>
</paragraph>
<paragraph>
<style name="bold">This is some text that is <bold>incorrect</bold></style>
<link>need to be linked </link>
<style name="bold"><bold> way </bold> to a document</style>
</paragraph>
任何帮助,非常感谢。
答
该转化:
<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