开发者

grouping xml elements based on content

i want to group elements from differents groups for example

input xml :

<corecom:ShipmentItemInstanceGenealogy>
  <corecom:ItemInstanceGenealogy>
    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715052-0007-S05546-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715052</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1J Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0007</corecom:SerialNumber>
        <corecom:LotNumber>S05546</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0001-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715052-0008-S05546-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715052</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1J Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0008</corecom:SerialNumber>
        <corecom:LotNumber>S05546</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0001-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715054-0007-S05548-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715054</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1G Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0007</corecom:SerialNumber>
        <corecom:LotNumber>S05548</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0001-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715054-0008-S05548-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715054</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1G Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0008</corecom:SerialNumber>
        <corecom:LotNumber>S05548</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0001-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>
  </corecom:ItemInstanceGenealogy>
</corecom:ShipmentItemInstanceGenealogy>

<corecom:ShipmentItemInstanceGenealogy>
  <corecom:ItemInstanceGenealogy>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715052-0009-S05546-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715052</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1J Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0009</corecom:SerialNumber>
        <corecom:LotNumber>S05546</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0002-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715052-0010-S05546-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715052</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1J Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0010</corecom:SerialNumber>
        <corecom:LotNumber>S05546</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0002-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corecom:ItemIdentification>
          <corecom:ID>715054-0009-S05548-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715054</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1G Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0009</corecom:SerialNumber>
        <corecom:LotNumber>S05548</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0002-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>

    <corecom:ItemInstanceGenealogyComposition>
      <corecom:ItemReference>
        <corec开发者_运维百科om:ItemIdentification>
          <corecom:ID>715054-0010-S05548-2</corecom:ID>
          <corecom:ContextID/>
          <corecom:ApplicationObjectKey>
            <corecom:ID>715054</corecom:ID>
          </corecom:ApplicationObjectKey>
          <corecom:AlternateObjectKey>
            <corecom:ID>Ea</corecom:ID>
          </corecom:AlternateObjectKey>
        </corecom:ItemIdentification>
        <corecom:Name>SK1G Serial OPSM item</corecom:Name>
        <corecom:ItemLotControlIndicator>true</corecom:ItemLotControlIndicator>
        <corecom:ItemSerialControlIndicator>5</corecom:ItemSerialControlIndicator>
        <corecom:SerialControlAgencyID>true</corecom:SerialControlAgencyID>
      </corecom:ItemReference>
      <corecom:ItemInstance>
        <corecom:SerialNumber>SGTIN-96/0010</corecom:SerialNumber>
        <corecom:LotNumber>S05548</corecom:LotNumber>
        <corecom:ExpirationDateTime/>
      </corecom:ItemInstance>
      <corecom:ParentItemInstanceGenealogyCompositionIdentification>
        <corecom:ID>717048-0002-S07546-1</corecom:ID>
      </corecom:ParentItemInstanceGenealogyCompositionIdentification>
    </corecom:ItemInstanceGenealogyComposition>
  </corecom:ItemInstanceGenealogy>
</corecom:ShipmentItemInstanceGenealogy>

i want to group them based on corecom:Name and corecom:SerialNumber tag

expected output:

<pasabm:TransactionProductVO>
  <pasabm:ItemNumber>SK1J Serial OPSM item</pasabm:ItemNumber>
  <pasabm:ItemSource>EBIZ_01</pasabm:ItemSource>
  <pasabm:ItemUom>Ea</pasabm:ItemUom>
  <pasabm:LotNumber>S05546</pasabm:LotNumber>
  <pasabm:LotSource/>
  <pasabm:LotLocation>M1</pasabm:LotLocation>
  <pasabm:ExpirationDate/>
  <pasabm:TransactionSerialAssocVO>
    <pasabm:ItemSerialNumber>0007</pasabm:ItemSerialNumber>
    <pasabm:SerialType>SGTIN-96</pasabm:SerialType>
    <pasabm:SerialIdentification>715052-0010-S05546-2</pasabm:SerialIdentification>
    <pasabm:ParentSerialIdentification>717048-0002-S07546-1</pasabm:ParentSerialIdentification>
    <pasabm:ShippedItemFlag>0</pasabm:ShippedItemFlag>
  </pasabm:TransactionSerialAssocVO>
  <pasabm:TransactionSerialAssocVO>
    <pasabm:ItemSerialNumber>0008</pasabm:ItemSerialNumber>
    <pasabm:SerialType>SGTIN-96</pasabm:SerialType>
    <pasabm:SerialIdentification>715052-0010-S05546-2</pasabm:SerialIdentification>
    <pasabm:ParentSerialIdentification>717048-0002-S07546-1</pasabm:ParentSerialIdentification>
    <pasabm:ShippedItemFlag>0</pasabm:ShippedItemFlag>
  </pasabm:TransactionSerialAssocVO>
  <pasabm:TransactionSerialAssocVO>
    <pasabm:ItemSerialNumber>0009</pasabm:ItemSerialNumber>
    <pasabm:SerialType>SGTIN-96</pasabm:SerialType>
    <pasabm:SerialIdentification>715052-0009-S05546-2</pasabm:SerialIdentification>
    <pasabm:ParentSerialIdentification>717048-0002-S07546-1</pasabm:ParentSerialIdentification>
    <pasabm:ShippedItemFlag>0</pasabm:ShippedItemFlag>
  </pasabm:TransactionSerialAssocVO>
  <pasabm:TransactionSerialAssocVO>
    <pasabm:ItemSerialNumber>0010</pasabm:ItemSerialNumber>
    <pasabm:SerialType>SGTIN-96</pasabm:SerialType>
    <pasabm:SerialIdentification>715052-0010-S05546-2</pasabm:SerialIdentification>
    <pasabm:ParentSerialIdentification>717048-0002-S07546-1</pasabm:ParentSerialIdentification>
    <pasabm:ShippedItemFlag>0</pasabm:ShippedItemFlag>
  </pasabm:TransactionSerialAssocVO>
</pasabm:TransactionProductVO>

my trial code:

          </pasabm:ExpirationDate>

         <xsl:for-each select="../../corecom:ItemInstanceGenealogyComposition/corecom:ItemReference">

                <pasabm:TransactionSerialAssocVO>
                  <pasabm:ItemSerialNumber>
                     <xsl:value-of select='substring-after(../corecom:ItemInstance/corecom:SerialNumber,"/")'/>

                  </pasabm:ItemSerialNumber>
                  <pasabm:SerialType>
                     <xsl:value-of select='substring-before(../corecom:ItemInstance/corecom:SerialNumber,"/")'/>
                  </pasabm:SerialType>
                  <pasabm:SerialIdentification>
                     <xsl:value-of select="corecom:ItemIdentification/corecom:ID"/>
                  </pasabm:SerialIdentification>
                  <pasabm:ParentSerialIdentification>
                      <xsl:value-of select="../corecom:ParentItemInstanceGenealogyCompositionIdentification/corecom:ID"/>
                   </pasabm:ParentSerialIdentification>
                   <pasabm:ShippedItemFlag>
                      <xsl:text disable-output-escaping="no">0</xsl:text>
                   </pasabm:ShippedItemFlag>
               </pasabm:TransactionSerialAssocVO>
              </xsl:if> 
           </xsl:for-each>


Here follows an XSLT 2.0 example using for-each-group. Because you have missed some important information in your question (and even because your input sample is huge), I've assumed a simpler input document:

<Root xmlns:corecom="corecom.com">
    <corecom:ShipmentItemInstanceGenealogy>
        <corecom:ItemInstanceGenealogy>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1J Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTIN-96/0007</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1J Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTIN-96/0008</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1G Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTINss-96/0007</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1G Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTINss-96/0008</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>
        </corecom:ItemInstanceGenealogy>

    </corecom:ShipmentItemInstanceGenealogy>

    <corecom:ShipmentItemInstanceGenealogy>
        <corecom:ItemInstanceGenealogy>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1J Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTIN-96/0009</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>

            <corecom:ItemInstanceGenealogyComposition>
                <corecom:ItemReference>
                    <corecom:Name>SK1G Serial OPSM item</corecom:Name>
                </corecom:ItemReference>
                <corecom:ItemInstance>
                    <corecom:SerialNumber>SGTINss-96/0009</corecom:SerialNumber>
                </corecom:ItemInstance>
            </corecom:ItemInstanceGenealogyComposition>

        </corecom:ItemInstanceGenealogy>
    </corecom:ShipmentItemInstanceGenealogy>
</Root>

When you use this transform against it:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:pasabm="baji.com"
    xmlns:corecom="corecom.com"
    xpath-default-namespace="corecom.com"
    exclude-result-prefixes="corecom"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/*">
        <pasabm:Root>

            <!-- first grouping -->
            <xsl:for-each-group select="*/*/*/ItemReference" 
                group-by="Name">
                <pasabm:TransactionProductVO>
                    <pasabm:ItemNumber>
                        <xsl:value-of select="current-grouping-key()"/> 
                    </pasabm:ItemNumber>
                    <!-- other stuff -->

                    <!-- second grouping -->
                    <xsl:for-each-group select="current-group()" 
                        group-by="../ItemInstance/SerialNumber">
                        <pasabm:TransactionSerialAssocVO>
                            <pasabm:ItemSerialNumber><xsl:value-of select="current-grouping-key()"/></pasabm:ItemSerialNumber>
                            <!-- other stuff -->
                        </pasabm:TransactionSerialAssocVO>              
                    </xsl:for-each-group>

                </pasabm:TransactionProductVO>
            </xsl:for-each-group>

        </pasabm:Root>
    </xsl:template>

</xsl:stylesheet>

You get:

<pasabm:Root xmlns:pasabm="baji.com">
   <pasabm:TransactionProductVO>
      <pasabm:ItemNumber>SK1J Serial OPSM item</pasabm:ItemNumber>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTIN-96/0007</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTIN-96/0008</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTIN-96/0009</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
   </pasabm:TransactionProductVO>
   <pasabm:TransactionProductVO>
      <pasabm:ItemNumber>SK1G Serial OPSM item</pasabm:ItemNumber>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTINss-96/0007</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTINss-96/0008</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
      <pasabm:TransactionSerialAssocVO>
         <pasabm:ItemSerialNumber>SGTINss-96/0009</pasabm:ItemSerialNumber>
      </pasabm:TransactionSerialAssocVO>
   </pasabm:TransactionProductVO>
</pasabm:Root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜