xpath - 为什么我的查询不起作用?

时间:2013-06-05 17:35:44

标签: xml database xpath xml-dtd

这是我的DTD + xml:

<!DOCTYPE test [
  <!ELEMENT team(owner+)>
  <!ATTLIST team
    name ID #REQUIRED
    coach IDREF #REQUIRED
    >
  <!ELEMENT owner      (#PCDATA)>
  <!ELEMENT coach    EMPTY>
  <!ATTLIST coach
    name ID #REQUIRED
    >
  <!ELEMENT game    EMPTY>
  <!ATTLIST game
    teams IDREFS #REQUIRED
    winner IDREF #REQUIRED
    >
  <!ELEMENT db    (coach*,team*,match*)>
]>


<?xml version="1.0" ?> 

    <db>
        <team name = "Hapoel" coach = "Abuksis">
            <owner> Eli Tabib</owner>
        </team>
        <team name = "Maccabi" coach = "Blat">
            <owner> Shimi</owner>
            <owner> Federman</owner>
        </team>
        <team name = "Beitar" coach = "Eli Cohen">
            <owner> Arkadi</owner>
        </team>
        <team name = "ElitzorLavi" coach = "Eli">
            <owner> Zehava</owner>
            <owner> Dani</owner>
        </team>
        <coach name = "Abuksis"/>
        <coach name = "Eli Cohen"/>
        <coach name = "Blat"/>
        <coach name = "Eli"/>
        <game teams = "Hapoel Maccabi" winner = "Maccabi"/>
        <game teams = "Hapoel Beitar" winner = "Beitar"/>
        <game teams = "Maccabi ElitzorLavi" winner = "Maccabi"/>
        <game teams = "Elitzor Lavi Maccabi" winner = "Maccabi"/>
    </db>

我必须找到比赛数量<2的游戏。 现在我希望只获得第一场比赛。 我试过这个Xpath:     分贝/游戏[计数(/ @队)2] 但它给了我所有的游戏..我的错误是什么?

2 个答案:

答案 0 :(得分:1)

xpath中存在几个问题。您应该按/分隔元素和属性。 ./@name只能缩短为@name。另外,如果您想要将名称作为结果,则最后必须使用/@name

db/team[count(owner)>1][not(@name=//game/@winner)]/@name

答案 1 :(得分:0)

问:我必须找到拥有一个以上所有者且从未赢过比赛的球队。

尝试:

db/team[count(owner)>1][not(@name = //game/@winner)]