javascript - 默认使用 xsl :when test? 返回所有元素

标签 javascript html xml xslt

我正在尝试制作一个网页,允许用户输入搜索 xml 文件。有多个输入,但在提交搜索时我无法容纳空输入。

var date = rDateSearch.value;   
if (date === "") {          
  var datein = "date=*";
} 
else {
  var datein = "date=" + date";
}

然后将“date”变量发送到 xsl:when 测试。我希望 xsl:when 返回所有日期值。

有办法做到这一点吗?我目前的想法行不通。

这是我的 xml 示例:

<record>
    <name>John Smith</name>
    <date>1972</date>
    <rating>0.5</rating>
</record>

这是我的 xsl。我不确定这段代码的适用性。

<xsl:for-each select="records/record">
    <xsl:choose>
        <xsl:when test ="name=*">
            <xsl:choose>
                <xsl:when test ="date=*">
                    <xsl:choose>
                        <xsl:when test ="rating=*">
                            <tr>
                                <td><xsl:value-of select="name"/></td>
                                <td><xsl:value-of select="date"/></td>
                                <td><xsl:value-of select="rating"/></td>
                            </tr>
                        </xsl:when>
                    </xsl:choose>
                </xsl:when>
            </xsl:choose>
        </xsl:when>
    </xsl:choose>
</xsl:for-each>

最佳答案

如果用户没有指定一个值来过滤名称、日期或评级,并且您正在尝试找出什么表达式将选择任何/全部,您可以使用:

xsl:when test="date"

只要在 xsl:for-each< 中评估的 record 元素中有一个 date 元素,就会“通过”测试.

或者,您可以使用:

xsl:when test="true()"

无论是否有日期元素,都会评估为 true。

相同的逻辑适用于其他元素。

关于javascript - 默认使用 xsl :when test? 返回所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43033493/

相关文章:

Javascript - 从另一个静态方法访问静态方法抛出 ReferenceError

javascript - Google Places 自动完成、地理编码和地址验证

xml - 获取所有行,即使节点并不总是存在

javascript - 如何在输入字段中存储和读取任意网址

javascript - 动态更改受类函数影响的类变量

javascript - HTML 表单框字段不会保持单击状态

html - 如何以编程方式检测缩放事件并在当前页面的 Chrome 中设置缩放?

python - Git word-diff 到 html

android - float 操作按钮未出现在 kitkat 中

c# - 在不加载到内存的情况下处理 xml