如何将非常复杂的XML扁平化为包含根级别所有节点的新XML

如何将非常复杂的XML扁平化为包含根级别所有节点的新XML

本文介绍了如何将非常复杂的XML扁平化为包含根级别所有节点的新XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试展平大型的可检索XML文档,以使所有嵌套元素都停留在根级别上,但获得一个附加的新属性("parent_id = ...")以仍然保持节点之间的关系. /p>

每个节点都有很多子节点,我也需要抓住这些子节点,因此内容必须保持不变.

文件非常大(50万行-大小为33 MB)

示例XML:

<product-catalog ...>
  <category id="1">
    <content>
      ...
     </content>
     <category id="2">
        <content>
        ...
        </content>
     </category>
     <category id="3">
        <content>
        ...
        </content>
        <category id="4">
           ...
        </category>
        <category id="5">
           ...
        </category>
     </category>
   </category>
</product-catalog>

所需的拼合输出:

<product-catalog>
  <category id="1" parent_id="0">
     <content>...</content>
  </category>
  <category id="2" parent_id="1">
     <content>...</content>
  </category>
  <category id="3" parent_id="1">
     <content>...</content>
  </category>
  <category id="4" parent_id="3">
     <content>...</content>
  </category>
  <category id="5" parent_id="3">
     <content>...</content>
  </category>
</product-catalog>

到目前为止已经尝试过了,但是它只提供根目录类别(实际上不是xslt-expert ...;))

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="category">
        <xsl:element name="category">
            <xsl:apply-templates select="@* | node() [not(child::category)]"/>
        </xsl:element>
    </xsl:template>


    <!-- remove -->
    <xsl:template match="translations" />


</xsl:stylesheet>
解决方案

请考虑以下示例:

XML

<product-catalog>
    <category id="1">
        <content>A1</content>
        <category id="2">
            <content>B</content>
        </category>
        <category id="3">
            <content>C1</content>
            <content>C2</content>
            <category id="4">
                <content>D</content>
            </category>
            <category id="5">
                <content>E</content>
            </category>
        </category>
        <content>A2</content>
    </category>
</product-catalog>

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/product-catalog">
    <xsl:copy>
        <xsl:apply-templates select="category"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="category">
    <category id="{@id}" parent_id="{parent::category/@id}">
        <xsl:copy-of select="content"/>
    </category>
    <xsl:apply-templates select="category"/>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<product-catalog>
  <category id="1" parent_id="">
    <content>A1</content>
    <content>A2</content>
  </category>
  <category id="2" parent_id="1">
    <content>B</content>
  </category>
  <category id="3" parent_id="1">
    <content>C1</content>
    <content>C2</content>
  </category>
  <category id="4" parent_id="3">
    <content>D</content>
  </category>
  <category id="5" parent_id="3">
    <content>E</content>
  </category>
</product-catalog>


尝试:

<xsl:template match="category">
    <category parent_id="{parent::category/@id}">
        <xsl:copy-of select="@* | content"/>
    </category>
    <xsl:apply-templates select="category"/>
</xsl:template>

I am currently trying to flatten a large recurisve XML document, so that all the nested elements stay on root level but get an additional new attribute ("parent_id=...") to still keep the relations between the nodes.

Each node has a lot of sub-nodes which i also need to grab, so the content has to stay the same.

The file is very large (500k Rows - 33 MB in Size)

Example XML:

<product-catalog ...>
  <category id="1">
    <content>
      ...
     </content>
     <category id="2">
        <content>
        ...
        </content>
     </category>
     <category id="3">
        <content>
        ...
        </content>
        <category id="4">
           ...
        </category>
        <category id="5">
           ...
        </category>
     </category>
   </category>
</product-catalog>

Required flattened output:

<product-catalog>
  <category id="1" parent_id="0">
     <content>...</content>
  </category>
  <category id="2" parent_id="1">
     <content>...</content>
  </category>
  <category id="3" parent_id="1">
     <content>...</content>
  </category>
  <category id="4" parent_id="3">
     <content>...</content>
  </category>
  <category id="5" parent_id="3">
     <content>...</content>
  </category>
</product-catalog>

tried this so far, but it only delivers root category (not really an xslt-expert... ;))

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="category">
        <xsl:element name="category">
            <xsl:apply-templates select="@* | node() [not(child::category)]"/>
        </xsl:element>
    </xsl:template>


    <!-- remove -->
    <xsl:template match="translations" />


</xsl:stylesheet>
解决方案

Consider the following example:

XML

<product-catalog>
    <category id="1">
        <content>A1</content>
        <category id="2">
            <content>B</content>
        </category>
        <category id="3">
            <content>C1</content>
            <content>C2</content>
            <category id="4">
                <content>D</content>
            </category>
            <category id="5">
                <content>E</content>
            </category>
        </category>
        <content>A2</content>
    </category>
</product-catalog>

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/product-catalog">
    <xsl:copy>
        <xsl:apply-templates select="category"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="category">
    <category id="{@id}" parent_id="{parent::category/@id}">
        <xsl:copy-of select="content"/>
    </category>
    <xsl:apply-templates select="category"/>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<product-catalog>
  <category id="1" parent_id="">
    <content>A1</content>
    <content>A2</content>
  </category>
  <category id="2" parent_id="1">
    <content>B</content>
  </category>
  <category id="3" parent_id="1">
    <content>C1</content>
    <content>C2</content>
  </category>
  <category id="4" parent_id="3">
    <content>D</content>
  </category>
  <category id="5" parent_id="3">
    <content>E</content>
  </category>
</product-catalog>


Try:

<xsl:template match="category">
    <category parent_id="{parent::category/@id}">
        <xsl:copy-of select="@* | content"/>
    </category>
    <xsl:apply-templates select="category"/>
</xsl:template>

这篇关于如何将非常复杂的XML扁平化为包含根级别所有节点的新XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:27