Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT,XML:如何将分组块分解成扁平的层次结构?_Xml_Xslt_Transformation - Fatal编程技术网

XSLT,XML:如何将分组块分解成扁平的层次结构?

XSLT,XML:如何将分组块分解成扁平的层次结构?,xml,xslt,transformation,Xml,Xslt,Transformation,我有以下带有一些嵌套元素的XML。 我需要帮助将此XML转换为平面层次结构 您可能也想看看这个问题: 提前感谢您的支持。 托马斯 原始XML: <transaction> <records type="1" > <record type="1" > <field number="1" > <item >223</item> </field>

我有以下带有一些嵌套元素的XML。 我需要帮助将此XML转换为平面层次结构

您可能也想看看这个问题:

提前感谢您的支持。 托马斯

原始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>&#x0A;</xsl:text>
        <transaction>
            <xsl:text>&#x0A;</xsl:text>
            <xsl:for-each select="//record">
                <xsl:copy-of select="." />
                <xsl:text>&#x0A;</xsl:text>
            </xsl:for-each>
            <xsl:text>&#x0A;</xsl:text>
        </transaction>
    </xsl:template>

</xsl:stylesheet>
这些标记是为了在输出XML中保留一些格式,但我不知道您是否对此感兴趣。如果没有,请随意移除

它的工作原理是使用for-each在输入XML中查找元素。select属性开头的//表示它可以匹配文档中的任何位置,而不仅仅是当前级别


然后,它只需使用的副本来插入在for each中找到的整个节点。

到目前为止,您得到了什么?我从Tim的回答中得到了XLST:@ThomasMuller您不是在尝试完全相反的操作吗?是的,完全正确:-