Hello stackoverflow社区,
我正在尝试按嵌套属性排序,但总是收到这样的错误消息
XPTY0004:不允许将一个以上项目作为第一个排序键。
这特别发生在行中
order by $x/role/@startdate
来自以下代码
<result>{
for $x in /congress/people/person
where $x/role[@type = 'sen' and @current = 1 and @state = 'NC']
order by $x/role/@startdate
return <senator name="{$x/@name}"/>
}</result>
我想知道您如何通过嵌套属性适当地排序。
编辑:
这是XML文档的一些示例代码
<congress>
<people>
<person birthday="1952-11-09" gender="M" id="B000944" name="Sherrod Brown">
<role district="13" enddate="1995-01-03" party="Democrat" startdate="1993-01-05" state="OH" type="rep"/>
<role district="13" enddate="1997-01-03" party="Democrat" startdate="1995-01-04" state="OH" type="rep"/>
<role enddate="2007-01-03" party="Democrat" startdate="2001-01-03" state="WA" type="sen"/>
<role enddate="2013-01-03" party="Democrat" startdate="2007-01-04" state="WA" type="sen"/>
<role current="1" enddate="2019-01-03" party="Democrat" startdate="2013-01-03" state="WA" type="sen"/>
</person>
<person>
...
</person>
</people>
</congress>
答案 0 :(得分:1)
在您的代码中
for $x in /congress/people/person
where $x/role[@type = 'sen' and @current = 1 and @state = 'NC']
order by $x/role/@startdate
我怀疑(尽管在猜测中)您想按与@startdate
条件匹配的role
中的where
进行排序。如果您确信只有这样一个角色,那么您可以写
for $x in /congress/people/person
let $role := $x/role[@type = 'sen' and @current = 1 and @state = 'NC']
where exists($role)
order by $role/@startdate
如果有一个以上的角色满足谓词的可能性,那么您仍然需要以某种方式指出您要对这些角色的开始日期进行排序。