首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将节点分成三个级别,并对一个节点求和

将节点分成三个级别,并对一个节点求和
EN

Stack Overflow用户
提问于 2012-07-24 16:42:46
回答 1查看 89关注 0票数 0

请遵循示例xml中的注释,它解释了我需要的一切,它让三个场景以相同的顺序检查输出xml中的每个场景。

输入XML

代码语言:javascript
复制
   <!-- In the below xml there will be one ASNInDesc with multiple ASNInPO's 
        and each ASNInPO contains one ASNInCtn and each ASNInCtn contains one ASNInItem -->

<ASNInDesc>
   <asn_nbr>ASN-1</asn_nbr>

   <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is same and item_id under
        ASNInItem is same. In this case two ASNInPO's has to be merged and two ASNInCtn's has to be merged
        into one tag(since the container_id is same) and two ASNInItem's has to be merged into one tag and unit_qty is to be added  -->

   <ASNInPO>
      <po_nbr>PO-1</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-1</container_id>
         <ASNInItem>
            <item_id>ITEM-1</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>
   <ASNInPO>
      <po_nbr>PO-1</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-1</container_id>
         <ASNInItem>
            <item_id>ITEM-1</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>

   <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is same and item_id under
        ASNInItem is different. In this case two ASNInPO's has to be merged and two ASNInCtn's has to be merged into one tag and
        two different ASNInItem's for the two different items  -->

    <ASNInPO>
      <po_nbr>PO-2</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-2</container_id>
         <ASNInItem>
            <item_id>ITEM-2</item_id>
            <unit_qty>3</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>
   <ASNInPO>
      <po_nbr>PO-2</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-2</container_id>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>3</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>

   <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is different and item_id under
        ASNInItem is different. In this case two ASNInPO's has to be merged into one tag with
        two different ASNInCtn's each having their own ASNInItem  -->

   <ASNInPO>
      <po_nbr>PO-3</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-3</container_id>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>
   <ASNInPO>
      <po_nbr>PO-3</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-4</container_id>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>
   </ASNInPO>

</ASNInDesc>

输出XML

代码语言:javascript
复制
<?xml version = '1.0' encoding = 'UTF-8'?>
<ASNInDesc>
   <asn_nbr>ASN-1</asn_nbr>
  <!-- Scenerio-1 --> 
   <ASNInPO>
      <po_nbr>PO-1</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-1</container_id>
         <ASNInItem>
            <item_id>ITEM-1</item_id>
            <unit_qty>4</unit_qty>
         </ASNInItem>        
      </ASNInCtn>
  </ASNInPO>
  <!-- Scenerio-2 -->  
  <ASNInPO>
      <po_nbr>PO-2</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-2</container_id>
         <ASNInItem>
            <item_id>ITEM-2</item_id>
            <unit_qty>3</unit_qty>
         </ASNInItem>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>3</unit_qty>
         </ASNInItem>        
      </ASNInCtn>
   </ASNInPO>
  <!-- Scenerio-3 -->   
   <ASNInPO>
      <po_nbr>PO-3</po_nbr>
      <ASNInCtn>
         <container_id>CONTAINER-3</container_id>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>
      <ASNInCtn>
         <container_id>CONTAINER-4</container_id>
         <ASNInItem>
            <item_id>ITEM-3</item_id>
            <unit_qty>2</unit_qty>
         </ASNInItem>
      </ASNInCtn>     
   </ASNInPO>

我尝试过使用我自己的xsl,但无法处理所有的场景。请帮我解决这个问题。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-08-03 13:09:21

下面是一个使用Muenchian grouping的XSLT1.0样式表。这是基于你的另一个问题的my answer

这是相同的方法,但是使用调整后的键,我们使用元素自己的id和父元素的id的串联来查找要分组的元素。例如,使用以下连接的键:concat(../../po_nbr, '|', ../container_id, '|', item_id)来查找ASNInItem元素。分隔符的选择并不重要,但您需要选择一个未出现在in中的字符。

此外,还添加了单位数量的总和。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

  <xsl:key name="ASNInPO" match="ASNInPO" 
           use="po_nbr"/>

  <xsl:key name="ASNInCtn" match="ASNInCtn" 
           use="concat(../po_nbr, '|', 
                       container_id)"/>

  <xsl:key name="ASNInItem" match="ASNInItem" 
           use="concat(../../po_nbr, '|', 
                       ../container_id, '|', 
                       item_id)"/>

  <xsl:template match="ASNInDesc">
    <xsl:copy>
      <xsl:copy-of select="asn_nbr"/>
      <xsl:apply-templates 
          select="ASNInPO[generate-id() =
                          generate-id(key('ASNInPO', 
                                          po_nbr)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ASNInPO">
    <xsl:copy>
      <xsl:copy-of select="po_nbr"/>
      <xsl:apply-templates 
          select="key('ASNInPO', po_nbr)/
                  ASNInCtn[generate-id() =
                           generate-id(key('ASNInCtn',
                                           concat(../po_nbr, '|', 
                                                  container_id))[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ASNInCtn">
    <xsl:copy>
      <xsl:copy-of select="container_id"/>
      <xsl:apply-templates
          select="key('ASNInCtn',
                      concat(../po_nbr, '|', 
                             container_id))/
                  ASNInItem[generate-id() =
                            generate-id(key('ASNInItem', 
                                        concat(../../po_nbr, '|',
                                               ../container_id, '|',
                                               item_id))[1])]"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="ASNInItem">
    <xsl:copy>
      <xsl:copy-of select="item_id"/>
      <unit_qty>
        <xsl:value-of 
            select="sum(key('ASNInItem', 
                        concat(../../po_nbr, '|', 
                               ../container_id, '|', 
                               item_id))/unit_qty)"/>
      </unit_qty>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11627209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档