XSLT,XML:如何将分组块分为平面层次结构?
问题描述:
我有以下XML与一些嵌套元素。 我需要帮助将此XML转换为平面层次结构。XSLT,XML:如何将分组块分为平面层次结构?
你可能想看看这个问题还有:提前 XSLT, XML: Grouping by attribute value
感谢您的支持。 托马斯
原始XML:
<transaction>
<records type="1" >
<record type="1" >
<field number="1" >
<item >223</item>
</field>
</record>
</records>
<records type="14" >
<record type="14" >
<field number="1" >
<item >777</item>
</field>
</record>
<record type="14" >
<field number="1" >
<item >555</item>
</field>
</record>
</records>
<record type="200" >
<field number="1" >
<item>546</item>
</field>
</record>
<record type="201" >
<field number="1" >
<item>123</item>
</field>
</record>
</transaction>
目标XML:
<transaction>
<record type="1" >
<field number="1" >
<item >223</item>
</field>
</record>
<record type="14" >
<field number="1" >
<item >777</item>
</field>
</record>
<record type="14" >
<field number="1" >
<item >555</item>
</field>
</record>
<record type="200" >
<field number="1" >
<item>546</item>
</field>
</record>
<record type="201" >
<field number="1" >
<item>123</item>
</field>
</record>
</transaction>
答
试试这个:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:text>
</xsl:text>
<transaction>
<xsl:text>
</xsl:text>
<xsl:for-each select="//record">
<xsl:copy-of select="." />
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</transaction>
</xsl:template>
</xsl:stylesheet>
的<xsl:text>
标签来保存一些格式的输出XML但我不知道你是否对此感兴趣。如果没有,请随时删除它们。
它通过使用for-each
来查找输入XML中的元素。 select
属性起始处的//
表示它可以匹配文档中的任何位置,而不仅仅是在当前级别。
然后它简单地使用copy-of
来插入在for-each
中找到的整个节点。
+0
史蒂夫,我的朋友很棒。非常感谢。 – ThomasMuller
到目前为止你有什么? – Steve
我从蒂姆的答案XLST:https://*.com/questions/46693291/xslt-xml-grouping-by-attribute-value – ThomasMuller
@ThomasMuller不是你想做的完全相反吗? – kumesana