문제

I have the following XML code :

<?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
    <article>
        <title>
            <![CDATA[The title of the test]]>
        </title>
        <date>
            <![CDATA[27/07/2013]]>
        </date>
        <url>
            <![CDATA[http://www.google.com]]>
        </url>
    </article>
    <article>
        <title>
            <![CDATA[The title of the SECOND test]]>
        </title>
        <date>
            <![CDATA[27/12/2013]]>
        </date>
        <url>
            <![CDATA[]]>
        </url>
    </article>
</articles>

I want to test if the string inside URL's CDATA is empty. In my XML example, I consider that the first URL node isn't empty but the second is an empty string.

Is it possible to test this with XPath in an XSL 1.0 stylesheet (I need to parse this XML with XSL) ?

I tried with the following XPath selectors :

string-length(/articles/article[2]/url)
string-length(/articles/article[2]/url/text())

The first selector result is 7, according to XML Tools in Notepad++, and 4 for the second one.

도움이 되었습니까?

해결책

Use the normalize-space function to strip starting and trailing whitespaces: http://www.w3.org/TR/xpath/#function-normalize-space

like this

string-length(normalize-space(/articles/article[2]/url))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top