开发者

XSLT contains() and empty tag

开发者 https://www.devze.com 2023-02-16 03:03 出处:网络
I have a problem with XSLT 1.0 contains() function and empty tags. I have this xslt stylesheet: <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">

I have a problem with XSLT 1.0 contains() function and empty tags.

I have this xslt stylesheet:

<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>

And the input is this:

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

Result is this:

<?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>

Problem here is that I expect the contains() function to return false if the <tester> tag is empty. However it is returning true for the last line. How can I avoid this, other than checkin开发者_如何学Gog the tag value first for emptiness and setting a variable to some value I know is not on the list of values?

Thanks!


This is the standard, as per W3C Spec behavior.

To avoid this, use:

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


XPath 1.0 spec is pretty vague on the matter, but XQuery 1.0 and XPath 2.0 Functions specification explicitly declares:

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

I don't think it's a something new, just a clarification of an old functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消