根据内容对xml元素进行分组

时间:2011-06-27 13:43:59

标签: xslt

我想对来自不同组的元素进行分组,例如

输入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>
        <corecom: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>

我想基于corecom对它们进行分组:Name和corecom:SerialNumber标记

预期产出:

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

我的试用代码:                                                                                                                                                                                                                                                                                                                                                    

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

1 个答案:

答案 0 :(得分:1)

以下是使用for-each-group的XSLT 2.0 示例。因为您错过了问题中的一些重要信息(甚至因为您的输入样本很大),我假设一个更简单的输入文档:

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

对它使用此转换时:

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

你得到:

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