2013-01-19 81 views
0

我有兩個for-each語句迭代,我希望輸出是在表內的垂直,而不是水平格式。XSL對於每個迭代

目前,它輸出:

a b c 
1 2 3 

我希望它輸出:

a 1 
b 2 
c 3 

有人可以幫助嗎?下面是XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="no"/> 
    <xsl:key name="names" match="Niche" use="."/> 
    <xsl:key name="niche" match="row" use="Niche"/> 

     <xsl:template match="/"> 
      <table border="1"> 

       <xsl:for-each select="//Niche[generate-id() = generate-id(key('names',.)[1])]"> 
       <xsl:sort select="."/> 

        <td> 
        <xsl:value-of select="."/> 
        </td> 
       </xsl:for-each> 

       <td><tr></tr></td> 

       <xsl:for-each select="//row[generate-id(.)=generate-id(key('niche', Niche)[1])]"> 
       <xsl:sort select="Niche"/> 

        <td> 
        <xsl:value-of select="count(key('niche', Niche))"/> 
        </td> 
       </xsl:for-each> 

      </table> 

    </xsl:template> 
</xsl:stylesheet> 

示例XML:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="sample.xsl" ?> 
<root> 
    <row> 
     <Niche>a</Niche> 
    </row> 
    <row> 
     <Niche>b</Niche> 
    </row> 
    <row> 
     <Niche>b</Niche> 
    </row> 
    <row> 
     <Niche>c</Niche> 
    </row> 
    <row> 
     <Niche>c</Niche> 
    </row> 
    <row> 
     <Niche>c</Niche> 
    </row> 
</root> 
+3

你能告訴我們一個你開始使用的XML的例子,很難解釋樣式表在做什麼而沒有看到它。 –

回答

2

我敢肯定,所有你需要做的是這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" indent="no"/> 
    <xsl:key name="niche" match="Niche" use="."/> 

    <xsl:template match="/"> 
    <table border="1"> 

     <xsl:for-each select="//Niche[generate-id(.)=generate-id(key('niche', .)[1])]"> 
     <xsl:sort select="."/> 
     <tr> 
      <td> 
      <xsl:value-of select="."/> 
      </td> 
      <td> 
      <xsl:value-of select="count(key('niche', .))"/> 
      </td> 
     </tr> 
     </xsl:for-each> 

    </table> 

    </xsl:template> 
</xsl:stylesheet> 

但請提供一個例子輸入XML等等我們可以驗證。

在採樣輸入,這將產生:

<table border="1"> 
    <tr> 
    <td>a</td> 
    <td>1</td> 
    </tr> 
    <tr> 
    <td>b</td> 
    <td>2</td> 
    </tr> 
    <tr> 
    <td>c</td> 
    <td>3</td> 
    </tr> 
</table> 

若要改爲排序從最高到最低的發生次數,你可以替換此行:

<xsl:sort select="."/> 

與此:

<xsl:sort select="count(key('niche', .))" order="descending" data-type="number"/> 
+0

我如何去排序最高的數字? 例如 c 3 b 2 a 1 – Accolade

+1

更新了我的回答。請參閱最後的評論。 – JLRishe

+0

似乎沒有工作,似乎按順序輸出左列?例如a 1 b 2 c 3 – Accolade