XQuery"展平"一个元素

时间:2015-06-08 20:28:46

标签: xquery

我从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 |法国"

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

fn:string-join($arg1 as xs:string*, $arg2 as xs:string)就是你在这里寻找的。

在您目前所需的用法中,它看起来如下所示:

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, "|"), "&#10;")

...及其输出:

France|UK