如何在XQuery中使用分隔符连接字符串而不使用尾随分隔符?

时间:2018-01-27 21:27:40

标签: xml xpath xquery

如何编写一些XQuery来解决以下问题:

这就是我目前所写的:

<friends>
{for $lastname in volunteerDatabase/person/name/lastname
return
concat($lastname/string(), ' +')}
</friends>

此查询返回:

<Friends>Geller + Bing + Tribbiani +</Friends>

我想返回以下内容,其中所有值都以'+'分隔:

<Friends>Geller + Bing + Tribbiani</Friends>

Dummy Data示例:

<?xml version="1.0" encoding="UTF-8" ?>

<volunteerDatabase>
<person age="31" ssn="046187254">
    <name>
        <firstname>Ross</firstname>
        <lastname>Geller</lastname>
    </name>
    <telephone type="landline">
        <number>5534567</number>
    </telephone>
    <telephone type="mobile">
        <number>0851234567</number>
    </telephone>
</person>

<person age="29" ssn="355817204">
    <name>
        <firstname>Chandler</firstname>
        <firstname>Muriel</firstname>
        <lastname>Bing</lastname>
    </name>
    <telephone type="mobile">
        <number>0869932617</number>
    </telephone>
</person>

<person ssn="778123666">
    <name>
        <firstname>Joseph</firstname>
        <firstname>Francis</firstname>
        <lastname>Tribbiani</lastname>
    </name>
    <telephone type="landline">
        <number>01628777</number>
    </telephone>
</person>
</volunteerDatabase>

任何指导意见。

1 个答案:

答案 0 :(得分:3)

不需要FLWOR表达式:

<friends>{fn:string-join(volunteerDatabase/person/name/lastname, " + ")}</friends>

...以文档作为上下文项运行时返回:

<friends>Geller + Bing + Tribbiani</friends>