XSL设置值复选框

时间:2017-09-21 16:01:27

标签: xslt

我有以下XSL来填充复选框,但value为空。我需要将值设置为相应的deptregioncategory。请帮忙。

这是xsl:

<xsl:key name="dept" match="/items/item/dept/text()" use="." />
<xsl:key name="region" match="/items/item/region/text()" use="." />
<xsl:key name="category" match="/items/item/category/text()" use="." />

<xsl:template match="/">
    <div class="w3-container w3-cell" style="background-color:#949494">
        <form>
            <xsl:for-each select="/items/item/dept/text()[generate-id()
                = generate-id(key('dept',.)[1])]">
                <input type="checkbox" name="dept" value="{dept}" />Department <xsl:value-of select="."/><br></br>
            </xsl:for-each>
            <br></br>
            <xsl:for-each select="/items/item/region/text()[generate-id()
                = generate-id(key('region',.)[1])]">
                <input type="checkbox" name="region" value="{region}" /> Region <xsl:value-of select="."/><br></br>
            </xsl:for-each>
            <br></br>
            <xsl:for-each select="/items/item/category/text()[generate-id()
                = generate-id(key('category',.)[1])]">
                <input type="checkbox" name="category" value="{category}" /> <xsl:value-of select="."/><br></br>
            </xsl:for-each>
            <br></br>
            <input type="text" placeholder="Search Text.." />
            <br></br><br></br>
            <input type="submit" value="Submit"/>
            <br></br><br></br>
        </form>
</xsl:template>

输出应为:

<input> type="checkbox" name="dept" value="dept1">Department 1
<input> type="checkbox" name="dept" value="dept1">Department 2
<input> type="checkbox" name="dept" value="region1">Region 1
<input> type="checkbox" name="dept" value="region2">Region 2
<input> type="checkbox" name="dept" value="category1">Category 1
<input> type="checkbox" name="dept" value="category2">Category 2

我的xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>
        <number>1</number>
        <dept>1</dept>
        <region>region1</region>
        <category>category1</category>
        <description>
            <pp>
                <line>
                    This is line 1 of description of item 1.
                </line>
                <line>
                    This is line 2 of description of item 1.
                </line>
                <line>
                    This is line 3 of description of item 1.
                </line>
            </pp>
        </description>
    </item>
    <item>
        <number>2</number>
        <dept>2</dept>
        <region>region1</region>
        <category>category2</category>
        <description>
            <pp>
                <line>
                    This is line 1 of description of item 2.
                </line>
                <line>
                    This is line 2 of description of item 2.
                </line>
                <line>
                    This is line 3 of description of item 2.
                </line>
            </pp>
        </description>
    </item>
    <item>
        <number>3</number>
        <dept>2</dept>
        <region>region2</region>
        <category>category2</category>
        <description>
            <pp>
                <line>
                    This is line 1 of description of item 3.
                </line>
                <line>
                    This is line 2 of description of item 3.
                </line>
                <line>
                    This is line 3 of description of item 3.
                </line>
            </pp>
        </description>
    </item>
</items>

1 个答案:

答案 0 :(得分:1)

问题在于这一行(以及类似的区域和类别)

<input type="checkbox" name="dept" value="{dept}" />

此时您已经定位在dept元素上,因此此代码正在查找名为dept的子元素。

要获取当前dept的值,而不是孩子,请执行此操作...

<input type="checkbox" name="dept" value="{.}" />