【问题标题】:XSLT when otherwise loop not working properlyXSLT 否则循环无法正常工作
【发布时间】:2017-09-27 15:09:27
【问题描述】:

我试图用 XSLT 表示一个输入字段可以是 textnumber 的表。我在 XML 中有一个包含列号和行号的标签。进入headerName标签,我有表格的表头信息。

这是我的 XML 示例:

<elements>
       <element type="TABLE">
            <id>table2</id>
            <order>49</order>
            <table>
                <colNumber>2</colNumber>
                <headerName>
                    <amount>false</amount>
                    <value>Header1</value>
                </headerName>
                <headerName>
                    <amount>true</amount>
                    <value>Header2</value>
                </headerName>
                <rowNumber>2</rowNumber>
            </table>
        </element>
</elements>

现在,我使用的 XSLT 是:

<xsl:for-each select="elements/element">
    <xsl:if test="@type='TABLE'">
        <div data-order="{order}" id="{id}">
            <table class="table table-bordered mt-lg">
                <thead>
                    <tr>
                        <xsl:for-each select="table/headerName">
                            <td>
                                <xsl:value-of select="value"/>
                            </td>
                        </xsl:for-each>
                    </tr>
                </thead>
                <tbody>
                    <xsl:variable name="rows" select="table/rowNumber/text()"/>
                    <xsl:variable name="cols" select="table/colNumber/text()"/>
                    <xsl:variable name="amount" select="table/headerName/amount/text()"/>
                    <xsl:for-each select="(//node())[$rows &gt;= position()]">
                        <tr>
                            <xsl:for-each select="(//node())[$cols &gt;= position()]">
                                <td>
                                    <xsl:choose>
                                        <xsl:when test="$amount = 'false'">
                                            <input type="text"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <input type="number"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
        </div>
    </xsl:if>
</xsl:for-each>

我的预期输出是这样的:

<div data-order="49" id="table2">
    <table class="table table-bordered mt-lg">
        <thead>
            <tr>
                <td>
                    Header1
                </td>
                <td>
                    Header2
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="text"></input></td>
                <td><input type="number"></input></td>
            </tr>
            <tr>
                <td><input type="text"></input></td>
                <td><input type="number"></input></td>
            </tr>
        </tbody>
    </table>
</div>

我没有看到错误在哪里,我尝试过使用 xpath 表达式,但我总是得到&lt;input type="text"/&gt;

【问题讨论】:

    标签: xml xslt xpath xslt-1.0


    【解决方案1】:

    当您设置变量 amount 时,您会在您的 xsl:for-each 语句之前这样做,因此它只会被设置为 XML 中第一个 table/headerName 的值您确实需要将声明移到里面最里面的xsl:for-each,看起来您希望它根据当前列号进行设置。

    但是,您需要考虑到此时您不会定位在 element 元素上,因此您需要首先将对 element 的引用存储在变量中,以便您可以访问它在xsl:for-each 里面

    试试这个 XSLT 片段

    <tbody>
        <xsl:variable name="rows" select="table/rowNumber/text()"/>
        <xsl:variable name="cols" select="table/colNumber/text()"/>
        <xsl:variable name="node" select="." />
        <xsl:for-each select="(//node())[$rows &gt;= position()]">
            <tr>
                <xsl:for-each select="(//node())[$cols &gt;= position()]">
                    <xsl:variable name="position" select="position()" />
                    <xsl:variable name="amount" select="$node/table/headerName[position() = $position]/amount/text()"/>
                    <td>
                        <xsl:choose>
                            <xsl:when test="$amount = 'false'">
                                <input type="text"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <input type="number"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
    </tbody>
    

    我猜你知道如果 rowNumbercolNumber 超过 XML 中的节点数,你的 XSLT 将会失败......

    【讨论】:

    • 您的解决方案完美运行,非常感谢您的解释,我以前没见过。
    猜你喜欢
    • 2019-01-01
    • 2014-12-02
    • 2013-08-09
    • 2013-10-30
    • 2013-10-19
    • 2017-02-03
    相关资源
    最近更新 更多