2013-06-05 65 views
-2

我DTD和XML:爲什麼我的xpath查詢不起作用?

<!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> 

我一定要找到比賽,使得球隊的數量是現在我希望能得到的只有第一場比賽。 我嘗試這個Xpath: db/game [count(/ @ teams)< 2] 但它給了我所有的遊戲.. 我的錯誤是什麼?

+0

可能重複的[xpath - 爲什麼我的查詢不起作用?](http://stackoverflow.com/questions/16946344/xpath-why-my-query-does-not-works) –

+0

相同的數據庫但其他查詢。需要幫助.. – user2456731

+0

然後嘗試學習基本概念,這將幫助您遠遠超過想要某人爲您解決這個特定問題。就像「給男人一條魚/教一個人釣魚」的寓言。 –

回答

0

表達式@teams表示名爲teams的屬性。表達式計數(@teams)計算具有該名稱的屬性的數量;自然,它對你輸入的每個遊戲的評價爲1,因爲你輸入的每個遊戲都有一個名爲teams的屬性。

如果要計算名爲團隊的屬性值中的令牌數量,則您的錯誤是計算該屬性的出現次數,而不是計算您想要計數的次數。您需要找到一種方法來標記屬性的值,然後計算令牌或其他方式來表示您需要測試的條件。 (提示:在空格標準化之後,帶有單個標記的值將不包含空格,具有多個標記的值將會被刪除。)

並注意Marc B的建議。

相關問題