I have an XML file like this :
<?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>
I want to check the queries :
-
All the people with no children (both
male&female) -
All the people with no male children (ie.
SONS)
I’ve tried :
-
/family/person[count(children)==0] -
/family/person[count(children)==0 and children!=male]
But it doesn’t work .
Can you please explain ? thanks .
The equals operator in XPath is “=”, not “==”.
Writing children!=male was presumably wishful thinking, you didn’t really imagine that would work, I hope.
Presumably someone with no children might be represented either by having no @children attribute or by having the attribute present but blank. You can cover both cases for your first query by testing
/family/person[normalize-space(@children)='']The second query is more difficult, because it involves a join. XPath 1.0 cannot handle every join query; XPath 2.0 can. You don’t say which you are using. Another complication is that the id() function can be a little bit erratic: the processing pipeline doesn’t always retain information about which attributes are IDs. But assuming id() works in your environment, you can do the second query (in either XPath 1.0 or 2.0) as