Xml 转换要删除重复项,请从重复项中提取一个元素
问题与此类似,, 迪米特已经回答了。在响应xml中有一个小小的修改。然而,这一次,我们需要从重复块中提取一个元素。不确定我们是否可以在这里使用xsl键函数 我的输入xmlXml 转换要删除重复项,请从重复项中提取一个元素,xml,xslt-1.0,Xml,Xslt 1.0,问题与此类似,, 迪米特已经回答了。在响应xml中有一个小小的修改。然而,这一次,我们需要从重复块中提取一个元素。不确定我们是否可以在这里使用xsl键函数 我的输入xml <M> <a> <b> <c f="123"> <key>Al</key> <e NO="678"> <f>Y<
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
艾尔
Y
FTO
艾尔
Y
FTO
艾尔
Y
FTO
我想要的输出xml—请注意,除了删除重复项之外,我们还需要从重复块中获取关键元素。可能相同,也可能不同
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
艾尔
艾尔
Y
FTO
艾尔
Y
FTO
此转换:
<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:key name="kAByC-F" match="a" use="*/c/@f"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"a[*/c
and
not(generate-id()
=
generate-id(key('kAByC-F', */c/@f)[1])
)
]"/>
<xsl:template match="a/b/c[@f]">
<c f="{@f}">
<xsl:apply-templates select="key('kAByC-F', @f)/b/c/key"/>
<xsl:apply-templates select="*[not(self::key)]"/>
</c>
</xsl:template>
</xsl:stylesheet>
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
应用于提供的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:key name="kAByC-F" match="a" use="*/c/@f"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"a[*/c
and
not(generate-id()
=
generate-id(key('kAByC-F', */c/@f)[1])
)
]"/>
<xsl:template match="a/b/c[@f]">
<c f="{@f}">
<xsl:apply-templates select="key('kAByC-F', @f)/b/c/key"/>
<xsl:apply-templates select="*[not(self::key)]"/>
</c>
</xsl:template>
</xsl:stylesheet>
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="123">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
<M>
<a>
<b>
<c f="123">
<key>Al</key>
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<key>Al</key>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse/>
</b>
</a>
</M>
按原样复制每个节点。它由两个匹配特定节点的模板覆盖
a
元素的a
元素a
元素的c
子元素棒 极 了谢谢你,迪米特。我仍在调试这些行,以便在流下运行。@Suresh:不客气。我用解释更新了问题。