0

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

这是 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>

提前致谢

4

1 回答 1

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>

说明

正确使用和覆盖身份规则

于 2012-11-22T21:37:26.947 回答