XSLT包含()和空标记

时间:2011-03-10 07:34:41

标签: xslt contains

我遇到了XSLT 1.0 contains()函数和空标记的问题。

我有这个xslt样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="test/tester">
            <test>
                <xsl:value-of select="."/>
                <xsl:text>: </xsl:text>
                <xsl:value-of select="contains('11,22,33', .)"/>
            </test>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输入是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <tester>11</tester>
    <tester>22</tester>
    <tester>33</tester>
    <tester>xx</tester>
    <tester/>
</test>

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<test>11: true</test>
<test>22: true</test>
<test>33: true</test>
<test>xx: false</test>
<test>: true</test>

问题在于,如果contains()标记为空,我希望<tester>函数返回false。但是它在最后一行返回true。我怎样才能避免这种情况,除了首先检查标签值是否空虚并将变量设置为某个值,我知道它不在值列表中?

谢谢!

2 个答案:

答案 0 :(得分:3)

根据W3C Spec行为,这是标准。

要避免这种情况,请使用

contains(',11,22,33,', concat(',', ., ','))

答案 1 :(得分:2)

XPath 1.0规范在这个问题上非常模糊,但是XQuery 1.0和XPath 2.0函数规范explicitly declares

If the value of $arg2 is the zero-length string, then the function returns true.

我不认为这是一个新的东西,只是对旧功能的澄清。