目前我正在试图拉平大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>