我有一个包含两种元素的 XML 文件:
<team name="n1" coach="c1"></team>
<game teams="t1 t2" winner="t2"></game>
我想选择所有没有赢得任何比赛的球队的教练。我试过的是
//team[count(//game[@winner=@name])=0]/@coach
但它不起作用。
我有一个包含两种元素的 XML 文件:
<team name="n1" coach="c1"></team>
<game teams="t1 t2" winner="t2"></game>
我想选择所有没有赢得任何比赛的球队的教练。我试过的是
//team[count(//game[@winner=@name])=0]/@coach
但它不起作用。
尝试
//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
这将为您提供未赢得任何比赛的球队的教练,包括未分配给任何球队的教练。
//coach[not(@name=//team[@name=//game/@winner]/@coach)]
我知道这可能看起来有点矫枉过正,但您可以使用 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" />