使用xslt将下一个子节点附加到父节点的内容

时间:2012-11-22 20:53:28

标签: xml xslt

我想将子节点附加到父节点。

这是XML。如果order_h下的order_no等于order_h中的以下order_no,我想将order_d附加到order_h

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <order>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783">

          <Size_b assoc_id="69398" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
            <size_d assoc_id="69398" ship_pack="BULK" ctn_sku="5602060" size_code="XS" alloc_qty="18" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
          </Size_b>
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783">
      </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783">

      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783">
                  </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783">
         </order_d>
    </order_h>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783">
                  </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783">
        </order_d>
    </order_h>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783">
      </order_d>
    </order_h>
  </order>
</document>

我需要输出如下

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <order>
      <order_h owner="GD" order_no="421424" profoma_po="421425">
         <order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421425" profoma_po="421426">
         <order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421426" profoma_po="421427">
         <order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421427" profoma_po="421428">
         <order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783"/>
      </order_h>
   </order>
</document>

提前致谢

1 个答案:

答案 0 :(得分:1)

此转化

<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="order_h|order_d/node()"/>

 <xsl:template match="order_h[1]">
  <xsl:copy>
   <xsl:apply-templates select=
    "@*|node()|following-sibling::*/node()"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档(更正为格式良好的XML文档):

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <order>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783">

          <Size_b assoc_id="69398" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
            <size_d assoc_id="69398" ship_pack="BULK" ctn_sku="5602060" size_code="XS" alloc_qty="18" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
          </Size_b>
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421424" profoma_po="421425">
      <order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783">
      </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783">

      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421425" profoma_po="421426">
      <order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783">
                  </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783">
         </order_d>
    </order_h>
    <order_h owner="GD" order_no="421426" profoma_po="421427">
      <order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783">
                  </order_d>
    </order_h>
  </order>
  <order>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783">
        </order_d>
    </order_h>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783">
      </order_d>
    </order_h>
    <order_h owner="GD" order_no="421427" profoma_po="421428">
      <order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783">
      </order_d>
    </order_h>
  </order>
</document>

会产生想要的正确结果:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <order>
      <order_h owner="GD" order_no="421424" profoma_po="421425">
         <order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421425" profoma_po="421426">
         <order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421426" profoma_po="421427">
         <order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783"/>
      </order_h>
   </order>
   <order>
      <order_h owner="GD" order_no="421427" profoma_po="421428">
         <order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783"/>
         <order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783"/>
      </order_h>
   </order>
</document>

<强>解释

正确使用和覆盖 identity rule