我有一个像这样的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT family (person)+>
<!ELEMENT person (name) >
<!ATTLIST person idnum ID #REQUIRED
gender (male | female) #REQUIRED
father IDREF #IMPLIED
mother IDREF #IMPLIED
children IDREFS #IMPLIED >
<!ELEMENT name (#PCDATA)>
<?xml version="1.0"?>
<!DOCTYPE family SYSTEM "family.dtd">
<family>
<person idnum = "T11" gender = "male" children ="T13 T14 T15"><name>11</name></person>
<person idnum = "T12" gender = "female" children ="T13 T14 T15"><name>12</name></person>
<person idnum = "T13" gender = "male" father="T11" mother="T12"><name>13</name></person>
<person idnum = "T14" gender = "male" father="T11" mother="T12"><name>14</name></person>
<person idnum = "T15" gender = "female" father="T11" mother="T12" children="T33"><name>15</name></person>
<person idnum = "T21" gender = "male" children="T23"><name>21</name></person>
<person idnum = "T22" gender = "female" children="T23"><name>22</name></person>
<person idnum = "T23" gender = "male" father="T21" mother="T22" children="T33"><name>23</name></person>
<person idnum = "T33" gender = "female" father="T23" mother="T15"><name>33</name></person>
</family>
我想查看查询:
所有没有孩子的人(male
&amp; female
)
所有没有男孩的人(即SONS
)
我试过了:
/family/person[count(children)==0]
/family/person[count(children)==0 and children!=male]
但它不起作用。
你可以解释一下吗?谢谢。答案 0 :(得分:2)
XPath中的equals运算符是“=”,而不是“==”。
写孩子!=男性大概是一厢情愿的想法,你真的没想到会有所作为,我希望。
据推测,没有孩子的人可能没有@children属性或者属性存在但是空白。您可以通过测试/family/person[normalize-space(@children)='']
第二个查询更难,因为它涉及连接。 XPath 1.0无法处理每个连接查询; XPath 2.0可以。你没有说你正在使用哪个。另一个复杂因素是id()函数可能有点不稳定:处理管道并不总是保留有关哪些属性是ID的信息。但假设id()在您的环境中有效,您可以执行第二次查询(在XPath 1.0或2.0中)
/family/person[not(id(@children)[@gender='male'])]
答案 1 :(得分:1)
这些xpath不正确。您正在尝试查找子元素,但它们是属性。
它应该是这样的:
//person[not(@children)]
答案 2 :(得分:1)