Search code examples
xqueryxslt-2.0

Replace function says item expected sequence found


I have the source XML like below, I am extracting title text with all the subelements string except "unwantedtag" content using except function. When I try to add the dot at the end of the title using the replace function. I am getting an error says item expected sequence found. Can anyone help me why I am getting this error and how to solve this:

Sample XML:

<test>
<title>sample <i>title</i><sup>*</sup> 
    <unwantedcontent>
        <p>unwanted text.</p>
    </unwantedcontent>
</title>
<p2>sampletest2</p2>
</test>

Xquery:

 for $k in (test/*:title)
 let $s1:= $k//text() except (.//*:unwantedcontent)//text()
 let $s2 := replace($s1, "(.*)([^.])$", "$1$2.")
 return $s2

Solution

  • The value of $s1 is going to comprise three text nodes ("sample ", "title", "*") plus possibly a couple of whitespace-only text nodes depending on how the source document was parsed. I think you probably want to concatenate these text nodes, and perhaps trim the trailing whitespace, before adding the ".". This would be

    let $s2 := replace(normalize-space(string-join($s1, '')), "(.*)([^.])$", "$1$2.")