XSLT、XML:如何将分组块分解为平面层次结构? [英] XSLT, XML: How to disentangle grouped blocks into a flat hierarchy?
问题描述
我有以下带有一些嵌套元素的 XML.我需要帮助将此 XML 转换为平面层次结构.
I have the following XML with some Nested elements. I need please help for converting this XML to a flat hierarchy.
您可能还想看看这个问题:XSLT、XML:按属性值分组
You may would like to take a look at this question as well: XSLT, XML: Grouping by attribute value
预先感谢您的支持.托马斯
Thanks in advance for your support. Thomas
原始 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 中的某些格式,但我不知道您是否对此感兴趣.如果没有,请随意删除它们.
The <xsl:text>
tags are to preserve some of the formatting in the output XML but I don't know if you're interested in that. Feel free to remove them if not.
它的工作原理是使用 for-each
在输入 XML 中查找元素.select
属性开头的 //
意味着它可以匹配文档中的任何地方,而不仅仅是在当前级别.
It works by using a for-each
to look for elements in the input XML. The //
at the start of the select
attribute means that it can match anywhere within the document, not just at the current level.
然后它简单地使用 copy-of
插入在 for-each
中找到的整个节点.
It then simply uses copy-of
to insert the entirety of the node found in the for-each
.
这篇关于XSLT、XML:如何将分组块分解为平面层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!