如何使用带有命名空间的 Xquery 从多个 XML 节点返回数据? [英] How to return data from multiple XML nodes using Xquery with a namespace?

查看:36
本文介绍了如何使用带有命名空间的 Xquery 从多个 XML 节点返回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the Microsoft namespace as below, how to return output like:

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

我在返回子句中遇到语法错误的地方:

Where I'm running into a syntax error with the return clause:

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 子句的方法,但我看到的示例并未使用此处的命名空间.

I've tried a few ways of wrapping the return clause with parenthesis or curly brackets, but the examples I've seen aren't using a namespace as here.

示例输出未将一个联系人与另一个联系人区分开来.

the example output isn't distinguishing one contact from another.

推荐答案

试试这个方法

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>

这篇关于如何使用带有命名空间的 Xquery 从多个 XML 节点返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆