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

如何将非常复杂的XML扁平化为包含根级别所有节点的新XML,xml,recursion,xslt,hierarchy,flatten,Xml,Recursion,Xslt,Hierarchy,Flatten,我目前正在尝试展平一个大型递归XML文档,以便所有嵌套元素都保持在根级别,但获得一个额外的新属性(“parent_id=…”),以保持节点之间的关系 每个节点都有很多子节点,我也需要抓取这些子节点,因此内容必须保持不变 文件非常大(500k行-33MB大小) XML示例: <product-catalog ...> <category id="1"> <content> ... </content> &l

我目前正在尝试展平一个大型递归XML文档,以便所有嵌套元素都保持在根级别,但获得一个额外的新属性(“parent_id=…”),以保持节点之间的关系

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

文件非常大(500k行-33MB大小)

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专家…;)


考虑以下示例:

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>

A1
B
C1
C2
D
E
A2
XSLT1.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>

A1
A2
B
C1
C2
D
E

如何复制
的所有现有属性并仅添加父id

尝试:



那么,你到底在哪里解决这个问题呢?到目前为止,你已经尝试过了(请参阅编辑的文章),但它只提供了看起来不错的根目录。最后一个问题-我如何复制所有现有属性并只添加父id(正如你已经做的那样)?@PhilippWiegel看到我的帖子中添加的内容。
<?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>