我正在尝试在XSL中弄清楚如何结束元素< bold>它是元素的父元素
编辑此外,元素<段落>的内容和< bold>得到一个<样式名称 =“黑体”取代。不包括< link>的内容元件。 <风格>将包含在<段>的内容中和< bold>。元素的内容<段落>可以有一个或多个< bold>和< link>
输入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>
非常感谢任何帮助。
答案 0 :(得分:0)
此转化:
<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>