-2

我的 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: db/game[count(/@teams)<2] 但它给了我所有游戏.. 我的错误是什么?

4

1 回答 1

0

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

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

并听取马克 B 的建议。

于 2013-06-05T19:18:43.043 回答