我有一个包含两种元素的XML文件:
<team name="n1" coach="c1"></team>
<game teams="t1 t2" winner="t2"></game>
我想选择那些没有赢过任何比赛的球队的教练。 我试过的是
//team[count(//game[@winner=@name])=0]/@coach
但它不起作用。
答案 0 :(得分:2)
尝试
//team[not(@name=//game/@winner)]/@coach
选择
COACHES OF ANY TEAM
//team /@coach
THAT DOES NOT
[not( )]
HAVE A @NAME EQUAL TO ANY @WINNER
@name=//game/@winner
答案 1 :(得分:0)
这将为您提供未赢得任何比赛的球队教练,包括未分配给任何球队的教练。
//coach[not(@name=//team[@name=//game/@winner]/@coach)]
答案 2 :(得分:0)
我知道它可能看起来有点过分,但您可以使用MS SQL 2005/2008 Express来测试您的xpath。 或者您可以使用http://sqlfiddle.com在线测试。
见这个例子:
DECLARE @xml XML
SET @xml ='<db>
<team name="chelsea" coach="mourinho"><owner>abramovich</owner><owner>abramovich2</owner></team>
<team name="arsenal" coach="venger"><owner>poorguy</owner></team>
<team name="manchester" coach="ferguson"><owner>richguy</owner></team>
<coach name="mourinho"></coach>
<coach name="venger"></coach>
<coach name="ferguson"></coach>
<coach name="nobody"></coach>
<game teams="chelsea arsenal" winner="arsenal"></game>
<game teams="manchester arsenal" winner="manchester"></game>
</db>'
SELECT
name = x.value('@name','varchar(100)')
, element = x.query('.')
FROM @xml.nodes('//coach[not(@name=//team[@name=//game/@winner]/@coach)]') AS T(x)
-- OUTPUT
name element
mourinho <coach name="mourinho" />
nobody <coach name="nobody" />