-1

使用如下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用括号或花括号来包装子句的方法,但是我看到的示例并没有像这里那样使用命名空间。

示例输出没有将一个联系人与另一个联系人区分开来。

4

1 回答 1

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>
于 2020-12-10T16:31:51.643 回答