【问题标题】:How to return data from multiple XML nodes using Xquery with a namespace?如何使用带有命名空间的 Xquery 从多个 XML 节点返回数据?
【发布时间】:2021-03-21 17:11:40
【问题描述】:

使用Microsoft命名空间如下,如何return输出如下:

<first>a</first>
<last>b</last>
<first>c</first>
<last>c</last>

我遇到了 return 子句的语法错误:

nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ basex sample.xq 
Stopped at /home/nicholas/flwor/sample.xq, 17/37:
[XPST0003] Expecting ')', found '>'.
nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ basex sample.full.xq 
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="First Name">a</S>
      <S N="Last Name">b</S>
      <S N="Emails">a@b;b@a.com</S>
      <S N="Phones">123 456-8904</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
  <Obj RefId="1">
    <TNRef RefId="0"/>
    <MS>
      <S N="First Name">c</S>
      <S N="Last Name">c</S>
      <S N="Emails">e@f.com</S>
      <S N="Phones">123456-3532;563 346-3453</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
</Objs>nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ cat sample.xq 

xquery version "3.1";


declare namespace ns1="http://schemas.microsoft.com/powershell/2004/04";


for $contact in db:open("sample")


let $first  := $contact//ns1:S[1][@N="First Name"]/data() 
let $last   := $contact//ns1:S[2][@N="Last Name"]/data() 
let $emails := $contact//ns1:S[3][@N="Emails"]/data() 
let $phones := $contact//ns1:S[4][@N="Phones"]/data() 


return (<first>{$first}</first><last>{$last}</last>)

nicholas@mordor:~/flwor$ 

我尝试了几种用括号或花括号括起 return 子句的方法,但我看到的示例没有使用这里的命名空间。

示例输出没有区分一个联系人和另一个联系人。

【问题讨论】:

    标签: xpath xquery xml-namespaces basex flwor


    【解决方案1】:

    试试这个方法

    for $contact in db:open("sample")//*:MS
    let $first := $contact//*:S[@N="First Name"]/text(),
      $last:= $contact//*:S[@N="Last Name"]/text()
    return (<first>{$first}</first>,<last>{$last}</last>)
    

    输出应该是

    <first>a</first>
    <last>b</last>
    <first>c</first>
    <last>c</last>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多