2012-10-26 31 views
3

從XML子元素值列表我有XML如下鮮明的使用XSL

<?xml version="1.0" encoding="UTF-8"?> 
<Set> 
<Bundles> 
    <BundleID>1</BundleID> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>34675</TrackingID> 
    </BundleDetails> 
    <BundleQty>12</BundleQty> 
</Bundles> 
<Bundles> 
    <BundleID>2</BundleID> 
    <BundleDetails> 
     <Classification>B</Classification> 
     <TrackingID>4563</TrackingID> 
    </BundleDetails> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>4563</TrackingID> 
    </BundleDetails> 
    <BundleQty>34</BundleQty> 
</Bundles> 
<Bundles> 
    <BundleID>3</BundleID> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>2343243</TrackingID> 
    </BundleDetails> 
    <BundleQty>22</BundleQty> 
</Bundles> 
<Bundles> 
    <BundleID>4</BundleID> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>123231</TrackingID> 
    </BundleDetails> 
    <BundleDetails> 
     <Classification>B</Classification> 
    </BundleDetails> 
    <BundleDetails> 
     <Classification>B</Classification> 
     <TrackingID>42342</TrackingID> 
    </BundleDetails> 
    <BundleQty>33</BundleQty> 
</Bundles> 
<Bundles> 
    <BundleID>5</BundleID> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>123231</TrackingID> 
    </BundleDetails> 
    <BundleDetails> 
     <Classification>A</Classification> 
     <TrackingID>42342</TrackingID> 
    </BundleDetails> 
    <BundleDetails> 
     <Classification>B</Classification> 
     <TrackingID>124512</TrackingID> 
    </BundleDetails> 
    <BundleQty>21</BundleQty> 
</Bundles> 
</Set> 

我需要爲每套/捆綁的細節如下

Bundle# Qty Classes 
1  12  A 
2  34  A,B 
3  22  A 
4  33  A,B 
5  21  A,B 

我開始如下,但打擊得到不同的班級名單。需要一些指導,

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

<xsl:template match="/"> 
    <html> 
    <body> 
    <p>Set Details</p> 
    <table> 
     <xsl:for-each select="Set/Bundles"> 
     <tr> 
      <td><xsl:value-of select="BundleID"/></td> 
      <td><xsl:value-of select="BundleQty"/></td> 
      <td>--Distinct List of ./BundleDetails/Classification </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

提前感謝您的幫助!

謝謝!

回答

2

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="kClassByValAndGrParent" match="Classification" 
    use="concat(generate-id(../..),'+', .)"/> 

<xsl:template match="*"><xsl:apply-templates/></xsl:template> 

<xsl:template match="/*"> 
    <table border="1"> 
    <tr> 
     <th>Bundle#</th><th>Qty</th><th>Classes</th> 
    </tr> 
    <xsl:apply-templates/> 
    </table> 
</xsl:template> 

<xsl:template match="Bundles"> 
    <tr> 
    <xsl:apply-templates select="*[not(self::BundleDetails)]"/> 
    <td> 
    <xsl:apply-templates select= 
     "BundleDetails/Classification 
         [generate-id() 
         = 
         generate-id(key('kClassByValAndGrParent', 
             concat(generate-id(../..),'+', .) 
            )[1] 
           ) 
         ] 
     "/> 
    </td> 
    </tr> 
</xsl:template> 

<xsl:template match="BundleID|BundleQty"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 

<xsl:template match="BundleDetails"/> 

<xsl:template match="Classification"> 
    <xsl:if test="position() > 1">,</xsl:if> 
    <xsl:value-of select="."/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<Set> 
    <Bundles> 
     <BundleID>1</BundleID> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>34675</TrackingID> 
     </BundleDetails> 
     <BundleQty>12</BundleQty> 
    </Bundles> 
    <Bundles> 
     <BundleID>2</BundleID> 
     <BundleDetails> 
      <Classification>B</Classification> 
      <TrackingID>4563</TrackingID> 
     </BundleDetails> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>4563</TrackingID> 
     </BundleDetails> 
     <BundleQty>34</BundleQty> 
    </Bundles> 
    <Bundles> 
     <BundleID>3</BundleID> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>2343243</TrackingID> 
     </BundleDetails> 
     <BundleQty>22</BundleQty> 
    </Bundles> 
    <Bundles> 
     <BundleID>4</BundleID> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>123231</TrackingID> 
     </BundleDetails> 
     <BundleDetails> 
      <Classification>B</Classification> 
     </BundleDetails> 
     <BundleDetails> 
      <Classification>B</Classification> 
      <TrackingID>42342</TrackingID> 
     </BundleDetails> 
     <BundleQty>33</BundleQty> 
    </Bundles> 
    <Bundles> 
     <BundleID>5</BundleID> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>123231</TrackingID> 
     </BundleDetails> 
     <BundleDetails> 
      <Classification>A</Classification> 
      <TrackingID>42342</TrackingID> 
     </BundleDetails> 
     <BundleDetails> 
      <Classification>B</Classification> 
      <TrackingID>124512</TrackingID> 
     </BundleDetails> 
     <BundleQty>21</BundleQty> 
    </Bundles> 
</Set> 

產生想要的,正確的結果

<table border="1"> 
    <tr> 
     <th>Bundle#</th> 
     <th>Qty</th> 
     <th>Classes</th> 
    </tr> 
    <tr> 
     <td>1</td> 
     <td>12</td> 
     <td>A</td> 
    </tr> 
    <tr> 
     <td>2</td> 
     <td>34</td> 
     <td>B,A</td> 
    </tr> 
    <tr> 
     <td>3</td> 
     <td>22</td> 
     <td>A</td> 
    </tr> 
    <tr> 
     <td>4</td> 
     <td>33</td> 
     <td>A,B</td> 
    </tr> 
    <tr> 
     <td>5</td> 
     <td>21</td> 
     <td>A,B</td> 
    </tr> 
</table> 

說明

  1. 正確使用的Muenchian Grouping Method

  2. 正確複合key定義指定任何Classification作爲其宏偉母體的函數,並且它的字符串值。

+0

+1我要添加的唯一東西就是'Classification'的'xsl:apply-templates'的'xsl:sort'。 –

+0

@DevNull,謝謝,你說得對。由於'Bundles'看起來已經被BundleID排序了,所以我不打算添加任何排序。我們甚至可以假設可能有多個具有相同「BundleID」的Bundles,然後我們應該添加另一個級別的分組,但這離真正的XML文檔太遠了。 –

+0

@DimitreNovatchev謝謝! – Sr7