如何扁平化一个非常复杂的XML转换成含根级别中所有节点一个新的XML(How to flatten

2019-09-28 07:32发布

目前我正在试图拉平大recurisve的XML文档,使所有的嵌套元素留在根级别,但得到额外的新属性(“PARENT_ID = ...”)仍然保持节点之间的关系。

每个节点都有大量的子节点,我还需要抓住的,所以内容必须保持不变。

该文件是非常大的(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的专家...;))

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

Answer 1:

请看下面的例子:

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>

我怎么能复制现有的所有属性<category...>和只添加PARENT_ID

尝试:

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


文章来源: How to flatten a very complex XML into a new XML containing all nodes at root level