Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 对一个类型的所有以前的元素进行计数,直至根_Xslt - Fatal编程技术网

Xslt 对一个类型的所有以前的元素进行计数,直至根

Xslt 对一个类型的所有以前的元素进行计数,直至根,xslt,Xslt,我正在尝试使用xslt从xml文档构建一个html表。xml代码如下所示: <root> <group> <name>A</name> <item>1</item> <item>2</item> <item>3</item> </group> <group>

我正在尝试使用xslt从xml文档构建一个html表。xml代码如下所示:

<root>
    <group>
        <name>A</name>
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </group>
    <group>
        <name>B</name>
        <item>4</item>
        <item>5</item>
    </group>
</root>
<table>
    <tr>
        <th>Group</th>
        <th>Item</th>
    </tr>
    <tr class="row_0">
        <td>A</td>
        <td>1</td>
    </tr>
    <tr class="row_1">
        <td>.</td>
        <td>2</td>
    </tr>
    <tr class="row_0">
        <td>.</td>
        <td>3</td>
    </tr>
    <tr class="row_1">
        <td>B</td>
        <td>4</td>
    </tr>
    <tr class="row_0">
        <td>.</td>
        <td>5</td>
    </tr>
</table>
<xsl:attribute name="class">row_<xsl:value-of select="count(all previous item elements) mod 2" /></xsl:attribute>
如何将
以前的所有项目元素
表示为真正的选择器?它还必须统计以前组中的所有
项目
元素。但是,它应该只计算到
root
(本例中的root不是我实际文档的
root

编辑: 我当前的xslt如下所示

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
    <xsl:template match="root">
        <table>
            <tr>
                <th>Group</th>
                <th>Item</th>
            </tr>

            <xsl:for-each select="group">
                <tr>
                    <xsl:attribute name="class">row_<xsl:value-of select="position() mod 2" /></xsl:attribute>
                    <td><xsl:value-of select="name" /></td>
                    <xsl:for-each select="item">
                        <xsl:if test="position() = 1">
                            <td><xsl:value-of select="text()" /></td>
                        </xsl:if>
                    </xsl:for-each>
                </tr>
                <xsl:for-each select="item">
                    <xsl:if test="position() > 1">
                        <tr>
                            <xsl:attribute name="class">row_<xsl:value-of select="position() mod 2" /></xsl:attribute>
                            <td>.</td>
                            <td><xsl:value-of select="text()" /></td>
                        </tr>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

团体
项目
划船_
划船_
.

上面写着
position()mod 2
的两个属性需要替换。

让我建议一种更简单的方法:

<xsl:template match="/root">
    <table>
        <tr>
            <th>Group</th>
            <th>Item</th>
        </tr>
        <xsl:for-each select="group/item">
            <tr class="row_{(position() - 1) mod 2}">
                <td>
                    <xsl:choose>
                        <xsl:when test="not(preceding-sibling::item)">
                            <xsl:value-of select="../name" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>&#160;</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
                <td><xsl:value-of select="."/></td>
            </tr>               
        </xsl:for-each>
    </table>
</xsl:template>

团体
项目
 

您是否已经有了生成表的XSLT样式表?如果是,请将其添加到您的问题中以节省我们的工作。您更简单的方法非常受欢迎!我对xslt了解不多,但必须在工作中使用它,knowbody和其他人也知道它。我只是应用了我从一些遗留代码中获得的东西。