将子节点附加到父节点

时间:2012-11-23 19:10:29

标签: xml xslt

我需要将子节点附加到父节点。 order_h下的Order_no等于下一个sibiling order_d应合并到父节点order_h

这是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 assoc_id="69398" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
              <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="5602001" size_code="S" alloc_qty="26" plan_pct_ratio="34" row_no="0" sort_value="6"/>
              </Size_b>
              <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="5602182" size_code="L" alloc_qty="11" plan_pct_ratio="15" row_no="0" sort_value="8"/>
              </Size_b>
              <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="5602078" size_code="M" alloc_qty="21" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
              </Size_b>
              <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>
            </size>
          </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">
            <size assoc_id="69400" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
              <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602019" size_code="S" alloc_qty="17" plan_pct_ratio="34" row_no="0" sort_value="6"/>
              </Size_b>
              <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602238" size_code="M" alloc_qty="14" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
              </Size_b>
              <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602131" size_code="XS" alloc_qty="12" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
              </Size_b>
              <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602027" size_code="L" alloc_qty="8" plan_pct_ratio="15" row_no="0" sort_value="8"/>
              </Size_b>
            </size>
          </order_d>
        </order_h>
    </order>
    </document>

并且预期的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 assoc_id="69398" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
      <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="5602001" size_code="S" alloc_qty="26" plan_pct_ratio="34" row_no="0" sort_value="6"/>
      </Size_b>
      <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="5602182" size_code="L" alloc_qty="11" plan_pct_ratio="15" row_no="0" sort_value="8"/>
      </Size_b>
      <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="5602078" size_code="M" alloc_qty="21" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
      </Size_b>
      <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>
    </size>
  </order_d>
  <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
    <size assoc_id="69400" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
      <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602019" size_code="S" alloc_qty="17" plan_pct_ratio="34" row_no="0" sort_value="6"/>
      </Size_b>
      <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602238" size_code="M" alloc_qty="14" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
      </Size_b>
      <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602131" size_code="XS" alloc_qty="12" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
      </Size_b>
      <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602027" size_code="L" alloc_qty="8" plan_pct_ratio="15" row_no="0" sort_value="8"/>
      </Size_b>
    </size>
  </order_d>
</order_h>
</document>

提前致谢

1 个答案:

答案 0 :(得分:2)

此转化

<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"/>

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

应用于提供的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 assoc_id="69398" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
                    <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="5602001" size_code="S" alloc_qty="26" plan_pct_ratio="34" row_no="0" sort_value="6"/>
                    </Size_b>
                    <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="5602182" size_code="L" alloc_qty="11" plan_pct_ratio="15" row_no="0" sort_value="8"/>
                    </Size_b>
                    <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="5602078" size_code="M" alloc_qty="21" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
                    </Size_b>
                    <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>
                </size>
            </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">
                <size assoc_id="69400" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
                    <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602019" size_code="S" alloc_qty="17" plan_pct_ratio="34" row_no="0" sort_value="6"/>
                    </Size_b>
                    <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602238" size_code="M" alloc_qty="14" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
                    </Size_b>
                    <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602131" size_code="XS" alloc_qty="12" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
                    </Size_b>
                    <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                        <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602027" size_code="L" alloc_qty="8" plan_pct_ratio="15" row_no="0" sort_value="8"/>
                    </Size_b>
                </size>
            </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">
            <size assoc_id="69398" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
               <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="5602001" size_code="S" alloc_qty="26" plan_pct_ratio="34" row_no="0" sort_value="6"/>
               </Size_b>
               <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="5602182" size_code="L" alloc_qty="11" plan_pct_ratio="15" row_no="0" sort_value="8"/>
               </Size_b>
               <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="5602078" size_code="M" alloc_qty="21" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
               </Size_b>
               <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>
            </size>
         </order_d>
         <order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
            <size assoc_id="69400" match_id="14" pack_code="BULK" size_range="2" model_name="SIZE SCALE 2" size_id="SIZE SCALE 2">
               <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                  <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602019" size_code="S" alloc_qty="17" plan_pct_ratio="34" row_no="0" sort_value="6"/>
               </Size_b>
               <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                  <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602238" size_code="M" alloc_qty="14" plan_pct_ratio="27.5" row_no="0" sort_value="7"/>
               </Size_b>
               <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                  <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602131" size_code="XS" alloc_qty="12" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
               </Size_b>
               <Size_b assoc_id="69400" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
                  <size_d assoc_id="69400" ship_pack="BULK" ctn_sku="5602027" size_code="L" alloc_qty="8" plan_pct_ratio="15" row_no="0" sort_value="8"/>
               </Size_b>
            </size>
         </order_d>
      </order_h>
   </order>
</document>