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

时间:2013-06-05 18:26:35

标签: xml database xpath 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" 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] 但它给了我所有的游戏.. 我的错是什么?

1 个答案:

答案 0 :(得分:0)

表达式@teams表示名为teams的属性。表达式count(@teams)计算具有该名称的属性数量;当然,对于输入中的每个游戏,它的评估结果为1,因为输入中的每个游戏都只有一个名为teams的属性。

如果要计算名为teams的属性值中的标记数,则错误是计算属性的出现次数,而不是计算要计算的值。您需要找到一种方法来标记属性的值,然后计算标记,或者表达您需要测试的条件的其他方式。 (提示:在空格规范化之后,具有单个标记的值将不包含空格。具有多个标记的值将会出现。)

并听取了Marc B的建议。