2012-07-24 4 views
0

예제 XML에서 의견을 따르십시오. 필요한 모든 것을 설명합니다. 세 가지 scenerio의 체크는 동일한 시퀀스의 출력 xml에있는 각 scenerio입니다.노드를 세 단계로 그룹화하고 하나의 노드 합계

입력 XML 난 내 자신의 XSL로 시도하지만, 모든 scenerios을 처리 할 수 ​​

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

<!-- 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. 이 문제를 해결해주세요. 미리 감사드립니다.

답변

0

다음은 Muenchian grouping을 사용하는 XSLT 1.0 스타일 시트입니다. 다른 질문에 my answer을 기반으로합니다.

이것은 동일한 접근 방식이지만 조정 된 키를 사용하여 요소의 고유 ID와 부모 요소의 ID를 연결하여 그룹화 요소를 조회합니다. 예를 들어, ASNInItem 요소는 다음 연결된 키를 사용하여 조회됩니다. concat(../../po_nbr, '|', ../container_id, '|', item_id). 구분 기호의 선택은 중요하지 않지만 ID에 나타나지 않는 문자를 선택해야합니다.

또한 단위 수량의 합계가 추가되었습니다.

<?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> 
관련 문제