XSLT,XML:如何将分组块拆分为平面层次结构?

时间:2017-10-16 10:07:10

标签: xml xslt transformation

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

您可能也想看看这个问题: XSLT, XML: Grouping by attribute value

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

原始XML:

mongoTemplate.aggregate(Aggregation.newAggregation(
            Aggregation.match(Criteria.where("owner").is(user).andOperator(Criteria.where("orderState").is("confirmed"))),
            ***Finding Difficulty in here***
            ), inputType, outputType)

目标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>

1 个答案:

答案 0 :(得分:1)

试试这个:

<?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>

<xsl:text>标记用于保留输出XML中的某些格式,但我不知道您是否对此感兴趣。如果没有,请随意删除它们。

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

然后只使用copy-of插入for-each中找到的整个节点。

相关问题