XSL - 将分层XML转换为Flat XML结构

时间:2018-04-09 20:26:59

标签: xml xslt xslt-1.0

我是XSL的新手,我一直在尝试压缩多层次的XML结构。我取得了一些成功,但已经撞墙了。我遗漏了Campaign_Product_Source_Group中的一些节点。节点为ITEM_GROUP_NAMEQUANTITY_AMOUNTINCLUDED

任何帮助将不胜感激..

这是我的XML结构:

<?xml version="1.0"?>
<Offers>
    <Records>
        <Campaign_Product_Result>
            <TABLE_ID>8</TABLE_ID>
             <REC_TYPE>4</REC_TYPE>
        <REC_VALUE>GroupName</REC_VALUE>
        <PARAM>1</PARAM>
        <COMPARE_TYPE>0</COMPARE_TYPE>
        <Campaign_Product_Result_Item>
            <TABLE_ID>8</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <REC_VALUE>987787</REC_VALUE>
            <PARAM/>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Result_Item>
        <Campaign_Product_Result_GTIN>
            <TABLE_ID>8</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <REC_VALUE>20054200000</REC_VALUE>
            <PARAM/>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Result_GTIN>
        </Campaign_Product_Result>
        <Campaign_Product_Source_Package>
            <TABLE_ID>9</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <QUANTITY_AMOUNT>Q1</QUANTITY_AMOUNT>
            <Campaign_Product_Source_Group>
                <TABLE_ID>9</TABLE_ID>
                <REC_TYPE>4</REC_TYPE>
                <ITEM_GROUP_NAME>GroupName</ITEM_GROUP_NAME>
                <QUANTITY_AMOUNT>1</QUANTITY_AMOUNT>
                <INCLUDED>0</INCLUDED>
                <Campaign_Product_Source_Item>
                    <TABLE_ID>9</TABLE_ID>
                    <REC_TYPE>0</REC_TYPE>
                    <REC_VALUE>02100074434</REC_VALUE>
                    <PARAM/>
                    <COMPARE_TYPE>0</COMPARE_TYPE>
                </Campaign_Product_Source_Item>
            </Campaign_Product_Source_Group>
        </Campaign_Product_Source_Package>
    </Records>
</Offers>

我的XSL:

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

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


<xsl:template 
match="Campaign_Product_Result
[Campaign_Product_Result_Item|Campaign_Product_Result_GTIN]">
    <xsl:copy>
        <xsl:apply-templates select="node()[(self::TABLE_ID)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_TYPE)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_VALUE)]|@*"/>
        <xsl:apply-templates select="node()[(self::PARAM)]|@*"/>
        <xsl:apply-templates select="node()[(self::COMPARE_TYPE)]|@*"/>
    </xsl:copy>
    <xsl:apply-templates select="Campaign_Product_Result_Item"/>
    <xsl:apply-templates select="Campaign_Product_Result_GTIN"/>
</xsl:template>

<xsl:template match="Campaign_Product_Source_Package">
    <xsl:copy>
      <xsl:apply-templates select="node()[(self::TABLE_ID)]|@*"/>
      <xsl:apply-templates select="node()[(self::REC_TYPE)]|@*"/>
      <xsl:apply-templates select="node()[(self::QUANTITY_AMOUNT)]|@*"/>
    </xsl:copy>
    <xsl:apply-templates select="Campaign_Product_Source_Package"/>
</xsl:template>

<xsl:template match="Campaign_Product_Source_Package[Campaign_Product_Source_Group]">
    <xsl:copy>
      <xsl:apply-templates select="node()[(self::TABLE_ID)]|@*"/>
      <xsl:apply-templates select="node()[(self::REC_TYPE)]|@*"/>
      <xsl:apply-templates select="node()[(self::ITEM_GROUP_NAME)]|@*"/>
      <xsl:apply-templates select="node()[(self::QUANTITY_AMOUNT)]|@*"/>
      <xsl:apply-templates select="node()[(self::INCLUDED)]|@*"/>
    </xsl:copy>
    <xsl:apply-templates select="Campaign_Product_Source_Group"/>
</xsl:template>

<xsl:template match="Campaign_Product_Source_Group
 [Campaign_Product_Source_Item]">
    <xsl:copy>
        <xsl:apply-templates select="node()[(self::TABLE_ID)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_TYPE)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_VALUE)]|@*"/>
        <xsl:apply-templates select="node()[(self::PARAM)]|@*"/>
        <xsl:apply-templates select="node()[(self::COMPARE_TYPE)]|@*"/>
    </xsl:copy>
    <xsl:apply-templates select="Campaign_Product_Source_Item"/>
</xsl:template>
</xsl:stylesheet>

我的输出:

<Offers>
    <Records>
        <Campaign_Product_Result>
            <TABLE_ID>8</TABLE_ID>
            <REC_TYPE>4</REC_TYPE>
            <REC_VALUE>GroupName</REC_VALUE>
            <PARAM>1</PARAM>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Result>
        <Campaign_Product_Result_Item>
            <TABLE_ID>8</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <REC_VALUE>987787</REC_VALUE>
            <PARAM/>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Result_Item>
        <Campaign_Product_Result_GTIN>
            <TABLE_ID>8</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <REC_VALUE>20054200000</REC_VALUE>
            <PARAM/>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Result_GTIN>
        <Campaign_Product_Source_Package>
            <TABLE_ID>9</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <QUANTITY_AMOUNT>Q1</QUANTITY_AMOUNT>
        </Campaign_Product_Source_Package>
        <Campaign_Product_Source_Group>
            <TABLE_ID>9</TABLE_ID>
            <REC_TYPE>4</REC_TYPE>
        </Campaign_Product_Source_Group>
        <Campaign_Product_Source_Item>
            <TABLE_ID>9</TABLE_ID>
            <REC_TYPE>0</REC_TYPE>
            <REC_VALUE>02100074434</REC_VALUE>
            <PARAM/>
            <COMPARE_TYPE>0</COMPARE_TYPE>
        </Campaign_Product_Source_Item>
    </Records>
</Offers>

1 个答案:

答案 0 :(得分:0)

它们丢失了,因为您没有将它们复制到适当的模板中:

<xsl:template match="Campaign_Product_Source_Group
 [Campaign_Product_Source_Item]">
    <xsl:copy>
        <xsl:apply-templates select="node()[(self::TABLE_ID)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_TYPE)]|@*"/>
        <xsl:apply-templates select="node()[(self::REC_VALUE)]|@*"/>
        <xsl:apply-templates select="node()[(self::PARAM)]|@*"/>
        <xsl:apply-templates select="node()[(self::COMPARE_TYPE)]|@*"/>
        <xsl:apply-templates select="node()[(self::ITEM_GROUP_NAME)]|@*"/>    <!-- added -->
        <xsl:apply-templates select="node()[(self::QUANTITY_AMOUNT)]|@*"/>    <!-- added -->
        <xsl:apply-templates select="node()[(self::INCLUDED)]|@*"/>           <!-- added -->
        <xsl:apply-templates select="Campaign_Product_Source_Item/*"/>        <!-- moved inside xsl:copy -->
    </xsl:copy>
</xsl:template>

我还将Campaign_Product_Source_Item/*元素移到了xsl:copy之间,以便将其展平。如果你愿意,可以改回来。

(剪切)输出为:

...
<Campaign_Product_Source_Group>
    <TABLE_ID>9</TABLE_ID>
    <REC_TYPE>4</REC_TYPE>
    <ITEM_GROUP_NAME>GroupName</ITEM_GROUP_NAME>
    <QUANTITY_AMOUNT>1</QUANTITY_AMOUNT>
    <INCLUDED>0</INCLUDED>
    <TABLE_ID>9</TABLE_ID>
    <REC_TYPE>0</REC_TYPE>
    <REC_VALUE>02100074434</REC_VALUE>
    <PARAM/>
    <COMPARE_TYPE>0</COMPARE_TYPE>
</Campaign_Product_Source_Group>
...
相关问题