Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
Xml 否则,XSLT循环无法正常工作_Xml_Xslt_Xpath_Xslt 1.0 - Fatal编程技术网

Xml 否则,XSLT循环无法正常工作

Xml 否则,XSLT循环无法正常工作,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,我试图用XSLT表示一个表,其中输入字段可以是text或number。我在XML中有一个包含列和行编号的标记。在headerName标记中,我有表格的标题信息 这是我的XML的一个示例: <elements> <element type="TABLE"> <id>table2</id> <order>49</order> <table

我试图用XSLT表示一个表,其中输入字段可以是
text
number
。我在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>

表2
49
2.
假的
校长1
真的
校长2
2.
现在,我使用的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>

校长1
校长2

我不知道哪里出了错,我尝试过使用xpath表达式,但在设置变量
amount
时,我总是得到
,在
xsl:for each
语句之前,因此,它只会被设置为XML中第一个
表/headerName
的值,您确实需要将声明移动到最内部的
xsl:for each
,因为看起来您希望根据当前列号设置它

但是,您需要考虑到此时您不会位于
元素
元素上,因此您需要首先在变量中存储对
元素
的引用,以便可以在
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>


我猜您知道,如果
行数
列数
超过XML中的节点数,XSLT将失败….

当您在
xsl:for each
语句之前设置变量
数量
时,因此,它只会被设置为XML中第一个
表/headerName
的值,您确实需要将声明移动到最内部的
xsl:for each
,因为看起来您希望根据当前列号设置它

但是,您需要考虑到此时您不会位于
元素
元素上,因此您需要首先在变量中存储对
元素
的引用,以便可以在
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>


我猜您已经意识到,如果
行数
列数
超过XML中的节点数,XSLT将失败….

您的解决方案工作得非常好,非常感谢您的解释,我以前没有看到过。您的解决方案工作得非常好,非常感谢您的解释,我以前没有看到过。