按属性值对元素进行分组 - XSLT 1.0

时间:2014-11-10 15:51:38

标签: html xml xslt

我正在尝试将this answer应用于我的案例,但我没有成功。

我想要做的是将具有相同位置的元素分组在另一个元素下,如下所示:

输入XML:

<ROOT>
    <ROW row_num="1" row_name="firstRow">
        <CELL position="1" id="515" price="15"/>
        <CELL position="2" id="466" price="22"/>
        <CELL position="3" id="539" price="4"/>
    </ROW>
    <ROW row_num="2" row_name="secondRow">
        <CELL position="1" id="1005" price="33.2"/>
        <CELL position="1" id="518" price="12.5"/>
        <CELL position="2" id="539" price="17"/>
        <CELL position="2" id="1016" price="19"/>
    </ROW>
    <ROW row_num="3" row_name="thirdRow">
        <CELL position="1" id="539" price="13"/>
        <CELL position="1" id="1150" price="34"/>
        <CELL position="1" id="458" price="18.2"/>
    </ROW>
</ROOT>

输出HTML:

<TABLE>
    <TR>
        <TD>1</TD>
        <TD>firstRow</TD>
        <TD>
            <DIV>515</DIV>
        </TD>
        <TD>
            <DIV>15</DIV>
        </TD>
        <TD>
            <DIV>466</DIV>
        </TD>
        <TD>
            <DIV>22</DIV>
        </TD>
        <TD>
            <DIV>539</DIV>
        </TD>
        <TD>
            <DIV>4</DIV>
        </TD>
    </TR>
    <TR>
        <TD>2</TD>
        <TD>secondRow</TD>
        <TD>
            <DIV>1005</DIV>
            <DIV>518</DIV>
        </TD>
        <TD>
            <DIV>33.2</DIV>
            <DIV>12.5</DIV>
        </TD>
        <TD>
            <DIV>539</DIV>
            <DIV>1016</DIV>
        </TD>
        <TD>
            <DIV>17</DIV>
            <DIV>19</DIV>
        </TD>
    </TR>
    <TR>
        <TD>3</TD>
        <TD>thirdRow</TD>
        <TD>
            <DIV>539</DIV>
            <DIV>1150</DIV>
            <DIV>458</DIV>
        </TD>
        <TD>
            <DIV>13</DIV>
            <DIV>34</DIV>
            <DIV>18.2</DIV>
        </TD>       
    </TR>
</TABLE>

所以我想要一个带有一些主要元数据的表,并且所有CELL都在一行和相同的位置,按位置分组到TD中并且不同。其他属性也一样。 ROW中的CELL数量可能不同,因此具有相同位置编号的CELL数量也可能不同。 CELL总是被订购,位置编号总是在ROW中以1开头。

这就是我得到的方式,但它并没有被诅咒:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="ROOT">
    <table>
        <xsl:apply-templates select="ROW"/>
    </table>
</xsl:template>
<xsl:template match="ROW">
    <tr>
        <td>
            <xsl:value-of select="@row_num" />
        </td>
        <td>
            <xsl:value-of select="@row_name" />
        </td>
        <xsl:apply-templates select="CELL"/>
    </tr>
</xsl:template>
<xsl:template match="CELL">
    <td>
        <div>
            <xsl:value-of select="@id" />
        </div>
    </td>
    <td>
        <div>
            <xsl:value-of select="@price" />
        </div>
    </td>
</xsl:template>
</xsl:stylesheet>

检查我的xsltcake here.

0 个答案:

没有答案
相关问题