在XSLT for XML Transformation中需要帮助

时间:2013-01-04 11:53:28

标签: xml xslt

我需要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>

1 个答案:

答案 0 :(得分:0)

你需要做一些分组 - 看看这篇SO帖子:Applying Muenchian grouping for a simple XML with XSLT。具体来说,请查看对Jeni Tennison's Muenchain grouping guide

的引用

我在这里假设只有一个request输出元素(否则你需要第二级分组),并且bomidpartid是绑定的。

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>