XSLT-如何删除标记并保留内容?
我有一个XML文件,类似于:XSLT-如何删除标记并保留内容?,xslt,xpath,xslt-1.0,Xslt,Xpath,Xslt 1.0,我有一个XML文件,类似于: <doc> <element> <word> <text>one word</text> </word> <word> <text>two words</text> </word> </element> <element> <other>
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
我只希望有一个标签和行的内容,当有两个在后面的时候,就像这样:
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<doc>
<element class="word">
<text>one word</text>
</element>
<element class="word">
<text>NN NN</text>
</element>
<element class="word">
<text>two words</text>
</element>
<element class="other">
<text>the sky</text>
</element>
</doc>
一个字
两个字
问题是我正在生成一个
,以获取
标记。这个
已经在一个
循环中,该循环将
标记按正确的顺序放置(对属性进行过滤)
我以前的XML文档如下所示:
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<doc>
<element class="word">
<text>one word</text>
</element>
<element class="word">
<text>NN NN</text>
</element>
<element class="word">
<text>two words</text>
</element>
<element class="other">
<text>the sky</text>
</element>
</doc>
一个字
NN NN
两个字
天空
任何帮助都会很好,谢谢:)
--更多细节--
这是我想要的结果:
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
我不能指定标签word或other,因为我没有封闭的标签列表。所以我使用的是xsl:variable:
<xsl:for-each select="element">
<xsl:variable name="name">
<xsl:value-of select="@class"/>
</xsl:variable>
<xsl:element name="{$name}">
<text>
<xsl:value-of select="."/>
</text>
</xsl:element>
</xsl:for-each>
只需使用
即可“展开”元素
XML输入
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<word>
<text>the sky</text>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
XSLT1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element">
<xsl:copy>
<word>
<xsl:apply-templates/>
</word>
</xsl:copy>
</xsl:template>
<xsl:template match="word|other">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
输出
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<word>
<text>the sky</text>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
此转换:
<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="element/*"/>
<xsl:template match="element/*[1]">
<word>
<xsl:apply-templates select="../*/*"/>
</word>
</xsl:template>
</xsl:stylesheet>
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<word>
<text>the sky</text>
<text>NN NN</text>
</word>
</element>
</doc>
应用于提供的XML文档时:
<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="element/*"/>
<xsl:template match="element/*[1]">
<word>
<xsl:apply-templates select="../*/*"/>
</word>
</xsl:template>
</xsl:stylesheet>
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<word>
<text>the sky</text>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
产生(我猜是)想要的、正确的结果:
<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="element/*"/>
<xsl:template match="element/*[1]">
<word>
<xsl:apply-templates select="../*/*"/>
</word>
</xsl:template>
</xsl:stylesheet>
<doc>
<element>
<word>
<text>one word</text>
</word>
<word>
<text>two words</text>
</word>
</element>
<element>
<other>
<text>the sky</text>
</other>
<word>
<text>NN NN</text>
</word>
</element>
</doc>
<doc>
<element>
<word>
<text>one word</text>
<text>two words</text>
</word>
</element>
<element>
<word>
<text>the sky</text>
<text>NN NN</text>
</word>
</element>
</doc>
一个字
两个字
天空
NN NN
试试这个:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="element/*">
<xsl:if test="not(preceding-sibling::*[name() = name(current())])">
<xsl:copy>
<xsl:apply-templates select="* | following-sibling::*[name()=name(current())]/*" />
</xsl:copy>
</xsl:if>
</xsl:template>
<!-- Identity template -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
它只需查找
元素
中以前没有同名同级节点(即每个同级节点中的第一个)的任何子节点,然后将模板应用于它自己的子节点以及以下同名同级节点的子节点。谢谢Dimitre的帮助!这个结果几乎就是我想要的,但是如果标签不一样,我不想应用这个规则。在本例中,结果可能会更改第二部分。但我一直在搜索:)谢谢你的快速回答。@user1759737,对不起,你的评论令人费解。你说“如果标签不同,我不想应用规则”是什么意思?你说“结果可能会改变第二部分”是什么意思“?我在帖子中提供了更多细节,我希望这能回答你的问题。谢谢您的帮助。@user1759737,即使在编辑之后,这个问题也几乎让人无法理解。请提供初始XML文档和最终想要的结果。还要解释转换必须实现的任何需求。不要向我们展示您当前的半个解决方案--您想要一个解决方案--并不是说任何解决方案都必须与您的相同。非常感谢您的回答!这正是我想要做的,但我的问题是,我无法在match属性中指定标记的名称,因为我使用获取标记的名称。