2016-03-03 35 views
0

我想從一些XML中產生一個HTML表格,通過SQL查詢。生成的XML如下所示:XML/XSLT屬性的嵌套循環產生HTML表格

<root> 
<form attribute1="1" attribute2="1" /> 
<form attribute1="1" attribute2="2" /> 
<form attribute1="1" attribute2="3" /> 
<form attribute1="2" attribute2="1" /> 
<form attribute1="2" attribute2="2" /> 
<form attribute1="3" attribute2="1" /> 
</root> 

表我試圖產生需要有一個與下排的每個attribute2,這樣的事情每一個獨特的ATTRIBUTE1標題行:

<attribute1="1" /> 
<attribute2="1" /> 
<attribute2="2" /> 
<attribute2="3" /> 

<attribute1="2" /> 
<attribute2="1" /> 
<attribute2="2" /> 

<attribute1="3" /> 
<attribute2="1" /> 

我沒有太多使用XML/XSLT的經驗,但我希望能夠通過表單循環執行某些操作,爲每個唯一屬性創建一個標題行1,然後創建與下面的唯一屬性1關聯的數據行。

+0

這是一個「分組」問題。如果您使用的是XSLT 1.0,則需要使用稱爲Muenchian分組的技術。請參閱http://www.jenitennison.com/xslt/grouping/muenchian.html獲取解釋。在這種情況下,您的密鑰將是'。如果您使用XSLT 2.0,則可以使用'xsl:for-each-group'。請參閱http://www.xml.com/pub/a/2003/11/05/tr.html。 –

+0

Hi @ Tim-C。我很確定我只使用1.0,所以我將不得不查看Muenchian分組。根據我的理解,這將根據屬性1爲每個行提供一個鍵。所以爲了繼續進行處理,我需要''來獲取標題行,然後在'獲取數據行? –

+0

我已經添加了一個答案來顯示Muenchian分組的實際應用 –

回答

0

如果您只能使用XSLT 1.0,試試這個XSLT對於初學者來說,它使用Muenchian分組的方法

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

    <xsl:key name="form" match="form" use="@attribute1" /> 

    <xsl:template match="root"> 
    <xsl:copy> 
     <!-- Get first "form" element that occurs in each group --> 
     <xsl:for-each select="form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <group> 
      <header><xsl:value-of select="@attribute1" /></header> 
      <!-- Get the "form" elements that make up the group --> 
      <xsl:apply-templates select="key('form', @attribute1)" /> 
      </group> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="form"> 
    <row><xsl:value-of select="@attribute2" /></row> 
    </xsl:template> 
</xsl:stylesheet> 

而且這裏的一些XSLT,它可以創建一個HTML表格。這是一個更先進的,因爲它計算出一行的最大列數,並使用它爲較短的行創建colspan屬性

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:key name="form" match="form" use="@attribute1" /> 

    <xsl:variable name="maxCells"> 
     <xsl:for-each select="/root/form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <xsl:sort select="count(key('form', @attribute1))" order="descending" /> 
      <xsl:if test="position() = 1"> 
       <xsl:value-of select="count(key('form', @attribute1))" /> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:variable> 

    <xsl:template match="root"> 
    <table border="1"> 
     <!-- Get first "form" element that occurs in each group --> 
     <xsl:for-each select="form[generate-id() = generate-id(key('form',@attribute1)[1])]"> 
      <tr> 
       <th colspan="{$maxCells}"> 
        <xsl:value-of select="@attribute1" /> 
       </th> 
      </tr> 
      <tr> 
      <!-- Get the "form" elements that make up the group --> 
       <xsl:apply-templates select="key('form', @attribute1)" /> 
      </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 

    <xsl:template match="form"> 
    <td> 
     <xsl:if test="position() = last()"> 
      <xsl:attribute name="colspan"> 
       <xsl:value-of select="$maxCells - count(key('form', @attribute1)) + 1" /> 
      </xsl:attribute> 
     </xsl:if> 
     <xsl:value-of select="@attribute2" /> 
    </td> 
    </xsl:template> 
</xsl:stylesheet> 
+0

Hi @TimC。我無法得到這個工作 - 它需要生成一個html我會用我試過的代碼編輯我的OP從你的。 –

+0

@DavidAustin我在我的答案中添加了一個示例,以顯示正在輸出的HTML表格。 –