0

我需要 XSLT 1.0 中的帮助将以下 Source XML 转换为 Result XML。对 XSLT 不是很熟悉,同时也在这个论坛上搜索有用的帖子。谢谢你的帮助。

源 XML:

<BomCollection>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1234EFG</messageid>
    <bomid>1234</bomid>
    <partid>4567</partid>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <linenumber>1</linenumber>
    <componentpartid>CID1</componentpartid>
    <componenttype>CTYPE</componenttype>
    <quantityperpart>2</quantityperpart>
  </BomAssyV>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1234EFG</messageid>
    <bomid>1234</bomid>
    <partid>4567</partid>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <linenumber>2</linenumber>
    <componentpartid>CID2</componentpartid>
    <componenttype>CTYPE</componenttype>
    <quantityperpart>30</quantityperpart>
  </BomAssyV>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1236EFG</messageid>
    <bomid>4321</bomid>
    <partid>8901</partid>
    <loc>MUM</loc>
    <country>IND</country>
    <cost>45000</cost>
    <linenumber>1</linenumber>
    <componentpartid>PID3</componentpartid>
    <componenttype>PTYPE</componenttype>
    <quantityperpart28></quantityperpart28>
  </BomAssyV>
</BomCollection>

结果 XML:

<request>
  <sender>Oracle</sender>
  <messageId>ABCD1234EFG</messageId>  
  <header>    
    <bomid>1234</bomid>
    <partId>4567</partId>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <line>
      <lineNumber>1</lineNumber>
      <componentPartId>CID1</componentPartId>      
      <componentType>CTYPE</componentType>    
      <quantityPerPart>2</quantityPerPart>      
    </line>
    <line>
      <lineNumber>2</lineNumber>
      <componentPartId>CID2</componentPartId>      
      <componentType>CTYPE</componentType>    
      <quantityPerPart>30</quantityPerPart>      
    </line>
  </header>
  <header>    
    <bomid>4321</bomid>
    <partId>8901</partId>
    <loc>MUM</loc>
    <country>IND</country>
    <cost>45000</cost>
    <line>
      <lineNumber>1</lineNumber>
      <componentPartId>PID3</componentPartId>      
      <componentType>PTYPE</componentType>    
      <quantityPerPart></quantityPerPart>      
    </line>   
  </header>
</request>
4

1 回答 1

0

您需要进行一些分组 - 请查看此 SO 帖子:使用 XSLT 对简单 XML 应用 Muenchian 分组。具体来说,看看对Jeni Tennison 的 Muenchain 分组指南的参考

我在这里假设只有一个request输出元素(否则您将需要第二级分组),并且bomid并且partid是并列的。

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

  <xsl:key name="bomids" match="BomAssyV" use="bomid" />

  <xsl:template match="BomCollection">
    <xsl:element name="request">
      <!-- We just scrape the first BomAssyV/sender and messageid elements -->
      <sender>
        <xsl:value-of select="BomAssyV/sender"/>
      </sender>
      <messageid>
        <xsl:value-of select="BomAssyV/messageid"/>
      </messageid>
      <!--i.e. Only the first bom in every group-->
      <xsl:apply-templates select="BomAssyV[generate-id()=generate-id(key('bomids', bomid)[1])]" mode="group"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="BomAssyV" mode="group">
    <header>
      <bomid>
        <xsl:value-of select="bomid"/>
      </bomid>
      <bomid>
        <xsl:value-of select="partid"/>
      </bomid>
      <loc>
        <xsl:value-of select="loc"/>
      </loc>
      <country>
        <xsl:value-of select="country"/>
      </country>
      <cost>
        <xsl:value-of select="cost"/>
      </cost>

      <xsl:for-each select="key('bomids', bomid)">
        <line>
          <lineNumber>
            <xsl:value-of select="linenumber"/>
          </lineNumber>
          <componentPartId>
            <xsl:value-of select="componentpartid"/>
          </componentPartId>
          <componentType>
            <xsl:value-of select="componenttype"/>
          </componentType>
          <quantityPerPart>
            <xsl:value-of select="quantityperpart"/>
          </quantityPerPart>
        </line>
      </xsl:for-each>
    </header>
  </xsl:template>

</xsl:stylesheet>

转换此输入 XML

<BomCollection>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1234EFG</messageid>
    <bomid>1234</bomid>
    <partid>4567</partid>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <linenumber>1</linenumber>
    <componentpartid>CID1</componentpartid>
    <componenttype>CTYPE</componenttype>
    <quantityperpart>2</quantityperpart>
  </BomAssyV>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1234EFG</messageid>
    <bomid>1234</bomid>
    <partid>4567</partid>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <linenumber>2</linenumber>
    <componentpartid>CID2</componentpartid>
    <componenttype>CTYPE</componenttype>
    <quantityperpart>30</quantityperpart>
  </BomAssyV>
  <BomAssyV>
    <sender>Oracle</sender>
    <messageid>ABCD1236EFG</messageid>
    <bomid>4321</bomid>
    <partid>8901</partid>
    <loc>MUM</loc>
    <country>IND</country>
    <cost>45000</cost>
    <linenumber>1</linenumber>
    <componentpartid>PID3</componentpartid>
    <componenttype>PTYPE</componenttype>
    <quantityperpart>28</quantityperpart>
  </BomAssyV>
</BomCollection>

到这个输出xml

<request>
  <sender>Oracle</sender>
  <messageid>ABCD1234EFG</messageid>
  <header>
    <bomid>1234</bomid>
    <bomid>4567</bomid>
    <loc>DEL</loc>
    <country>IND</country>
    <cost>30</cost>
    <line>
      <lineNumber>1</lineNumber>
      <componentPartId>CID1</componentPartId>
      <componentType>CTYPE</componentType>
      <quantityPerPart>2</quantityPerPart>
    </line>
    <line>
      <lineNumber>2</lineNumber>
      <componentPartId>CID2</componentPartId>
      <componentType>CTYPE</componentType>
      <quantityPerPart>30</quantityPerPart>
    </line>
  </header>
  <header>
    <bomid>4321</bomid>
    <bomid>8901</bomid>
    <loc>MUM</loc>
    <country>IND</country>
    <cost>45000</cost>
    <line>
      <lineNumber>1</lineNumber>
      <componentPartId>PID3</componentPartId>
      <componentType>PTYPE</componentType>
      <quantityPerPart>28</quantityPerPart>
    </line>
  </header>
</request>
于 2013-01-04T15:33:52.747 回答