Smillee
Smillee

Reputation: 21

XQuery list nodes contains all attributes

I have xml data file like this.

<info>
  <property id="p1" type="land">
    <name>Time Square</name>
    <owner type="person" id="x01" />
    <owner type="company" id="x01" />
  </property>
  <property id="p2" type="building">
    <name>HBE Complex</name>
    <owner type="company" id="x01" />
    <owner type="company" id="x02" />
    <owner type="person" id="x02" />
  </property>
  <property id="p3" type="land">
    <name>UNE Home</name>
    <owner type="company" id="x02" />
    <owner type="person" id="x02" />
  </property>
  <!-- more properties -->
</info>

I want to list all company's id that have all types of property. e.g. company id=x02 ,that has land and building.

Now, I'dont know how to write it but I feel may have to use every in satisfies to check every elements. I will update my code if I figure out some more.

Are there any ideas to look up the attributes across elements.

Upvotes: 2

Views: 497

Answers (1)

Jens Erat
Jens Erat

Reputation: 38662

Find all distinct company ids, and for each of them look up all properties and see if there are both of types building and land.

for $id in distinct-values(//owner[@type='company']/@id)
let $properties := //property[owner[@type='company' and @id=$id]]
where $properties/@type='land' and $properties/@type='building'
return $id

satisfies implements a kind of implicit loop. You never need it, but sometimes in can result in more readable code. You could use satisfies for example like this:

(: snip :)
where every $type in ('land', 'building') satisfies $properties/@type=$type
(: snip :)

This would be of special interest if you're having more than this set of property types to check against.

Upvotes: 2

Related Questions