我从XML文件中提取数据,我需要提取一个分隔的子元素列表。我有以下内容:
for $record in //record
let $person := $record/person/names
return concat($record/@uid/string()
,",", $record/@category/string()
,",", $person/first_name
,",", $person/last_name
,",", $record/details/citizenships
," ")
元素" citizenships"包含称为"公民身份的子元素"并且当查询站立时,它将它们全部粘在一起,例如, " UKFrance&#34 ;.我需要将它们保存在一个字符串中但是将它们分开,例如" UK |法国"
提前感谢您的帮助!
答案 0 :(得分:1)
fn:string-join($arg1 as xs:string*, $arg2 as xs:string)就是你在这里寻找的。 p>
在您目前所需的用法中,它看起来如下所示:
fn:string-join($record/details/citizenships/citizenship, "|")
在文档外进行测试,包括:
fn:string-join(("UK", "France"), "|")
...返回:
UK|France
值得注意的是,("UK", "France")
是一个字符串序列,就像返回多个citizenship
的查询同样是一个序列(在传递时将对其string value进行评估的条目到fn:string-join()
,它被输入为第一个参数的字符串序列。)
考虑以下(简化)查询:
declare context item := document { <root>
<record uid="1">
<person>
<citizenships>
<citizenship>France</citizenship>
<citizenship>UK</citizenship>
</citizenships>
</person>
</record>
</root> };
for $record in //record
return concat(fn:string-join($record//citizenship, "|"), " ")
...及其输出:
France|UK