在父节点下添加多个子节点

时间:2012-11-28 22:22:14

标签: xml xslt

我想在order_h中添加多个子节点order_d in order_h匹配order_h下的order_no。

这是我的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="421425" 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_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>

这就想要我最近使用的xslt从我之前发布的帖子

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

预期输出

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <order>
      <order_h order_no="421424" owner="GD" profoma_po="421425">
         <order_d color_code="730" item_no="100000014" line_no="1" manufacturer="100783"
                  order_no="421424"
                  owner="GD">
            <size assoc_id="69398" match_id="14" model_name="SIZE SCALE 2" pack_code="BULK"
                  size_id="SIZE SCALE 2"
                  size_range="2">
               <Size_b assoc_id="69398" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="26" assoc_id="69398" ctn_sku="5602001" plan_pct_ratio="34"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="S"
                          sort_value="6"/>
               </Size_b>
               <Size_b assoc_id="69398" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="11" assoc_id="69398" ctn_sku="5602182" plan_pct_ratio="15"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="L"
                          sort_value="8"/>
               </Size_b>
               <Size_b assoc_id="69398" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="21" assoc_id="69398" ctn_sku="5602078" plan_pct_ratio="27.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="M"
                          sort_value="7"/>
               </Size_b>
               <Size_b assoc_id="69398" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="18" assoc_id="69398" ctn_sku="5602060" plan_pct_ratio="23.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="XS"
                          sort_value="5"/>
               </Size_b>
            </size>
         </order_d>
         <order_d color_code="002" item_no="100000014" line_no="3" manufacturer="100783"
                  order_no="421424"
                  owner="GD">
            <size assoc_id="69400" match_id="14" model_name="SIZE SCALE 2" pack_code="BULK"
                  size_id="SIZE SCALE 2"
                  size_range="2">
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="17" assoc_id="69400" ctn_sku="5602019" plan_pct_ratio="34"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="S"
                          sort_value="6"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="14" assoc_id="69400" ctn_sku="5602238" plan_pct_ratio="27.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="M"
                          sort_value="7"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="12" assoc_id="69400" ctn_sku="5602131" plan_pct_ratio="23.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="XS"
                          sort_value="5"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="8" assoc_id="69400" ctn_sku="5602027" plan_pct_ratio="15" row_no="0"
                          ship_pack="BULK"
                          size_code="L"
                          sort_value="8"/>
               </Size_b>
            </size>
         </order_d>
         </order_h>
         <order_h order_no="421425" owner="GD" profoma_po="421425">
         <order_d color_code="002" item_no="100000014" line_no="3" manufacturer="100783"
                  order_no="421424"
                  owner="GD">
            <size assoc_id="69400" match_id="14" model_name="SIZE SCALE 2" pack_code="BULK"
                  size_id="SIZE SCALE 2"
                  size_range="2">
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="17" assoc_id="69400" ctn_sku="5602019" plan_pct_ratio="34"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="S"
                          sort_value="6"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="14" assoc_id="69400" ctn_sku="5602238" plan_pct_ratio="27.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="M"
                          sort_value="7"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="12" assoc_id="69400" ctn_sku="5602131" plan_pct_ratio="23.5"
                          row_no="0"
                          ship_pack="BULK"
                          size_code="XS"
                          sort_value="5"/>
               </Size_b>
               <Size_b assoc_id="69400" pct_ratio="PCT" priority="0" row_no="0" ship_pack="BULK"
                       total_pct="100">
                  <size_d alloc_qty="8" assoc_id="69400" ctn_sku="5602027" plan_pct_ratio="15" row_no="0"
                          ship_pack="BULK"
                          size_code="L"
                          sort_value="8"/>
               </Size_b>
            </size>
         </order_d>
      </order_h>
   </order>
</document>

有人可以告诉我如何获得预期的输出。 @Dimitre很抱歉没有在我之前的帖子中解释我的确切要求。

谢谢你。

1 个答案:

答案 0 :(得分:1)

如果您使用的是XSLT1.0,那么这是一项名为Muenchian Grouping的技术。您是按 order_no 属性对 order_h 元素进行分组,在这种情况下,您可以定义以下键:

 <xsl:key name="order" match="order_h" use="@order_no" />

接下来,对于每个 order_h 元素,您需要匹配组中首先出现的给定 order_no 属性的元素。您可以按如下方式执行此操作:

<xsl:template match="order_h[generate-id() = generate-id(key('order', @order_no)[1])]">

为了解释这是如何工作的,表达式key('order', @order_no)[1]返回当前属性的键中的第一个元素。然后,将其与当前元素进行比较,看它是否匹配。

在此模板中,您可以复制元素,并输出所有子元素,如此

<xsl:apply-templates select="key('order', @order_no)/*" />

您还需要一个模板来匹配其他 order_h 元素,以便它们不会输出两次:

<xsl:template match="order_h" />

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:key name="order" match="order_h" use="@order_no" />

   <xsl:template match="order_h[generate-id() = generate-id(key('order', @order_no)[1])]">
      <order_h>
         <xsl:apply-templates select="@*"/>
         <xsl:apply-templates select="key('order', @order_no)/*" />
      </order_h>
   </xsl:template>

   <xsl:template match="order_h" />

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

这应该可以为您提供所需的输出。

如果您使用的是XSLT2.0,则可以使用 xsl:for-each-group 来实现相同的目标

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="order">
      <order>
         <xsl:for-each-group select="order_h" group-by="@order_no">
            <order_h>
               <xsl:apply-templates select="@*"/>
               <xsl:apply-templates select="current-group()/*"/>
            </order_h>
         </xsl:for-each-group>
      </order>
   </xsl:template>

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