views:

145

answers:

2
<listings>
   <property rln="r317080" firm="f102" agent="a2140">
      <street>2638 Maple Avenue</street>
      <city>Padua</city>
      <state>WI</state>
      <zip>53701</zip>
      <price>229000</price>
      <style>2 Story Contemporary, Transitional</style>
      <sqfeet>2328</sqfeet>
      <bathrooms>2 1/2</bathrooms>
      <bedrooms>4</bedrooms>
      <garage>2 car, attached</garage>
      <age>22</age>
      <description>Very nice home on a one block dead end street with woods nearby. 
      Very special location for quiet and privacy! Home features open floor plan with 
      large rooms - new patio doors to pretty yard. updates: shingles, vinyl siding, 
      refrig and dishwasher, garage door. Fireplace in family room flanked by great 
      built-ins. add first floor laundry and award winning Padua schools.
      </description> 
   </property>
   <property ...>
      <city>Broxton</city>
    ...
   </property>
   <property ...>
      <city>Cutler</city>
    ...
   </property>
   <property ...>
      <city>Argyle</city>
    ...
   </property>
   <property ...>
      <city>Stratmore</city>
    ...
   </property>
   <property ...>
      <city>Padua</city>
    ...
   </property>
   <property ...>
      <city>Oseola</city>
    ...
   </property>
   <property ...>
      <city>Fenmore</city>
    ...
   </property>
   <property ...>
      <city>Cutler</city>
    ...
   </property>
   <property ...>
      <city>Padua</city>
    ...
   </property>
   <property ...>
      <city>Cutler</city>
    ...
   </property>
   <property ...>
      <city>Oseola</city>
    ...
   </property>
</listings>

In my textbook (XML 2nd Edition by Patrick Carey) it provides an example of using 'Muenchian Grouping' to find unique selections. The part I don't understand is thus:

It gets to here, in the progression of the example where it states: " property[generate-id()=generate-id(key("cityNames", "Cutler")[1])] " which says that this will find the first 'Cutler' in the selection, due to the index of '[1]'. Which given the XML above will return "Cutler"

Now the example progresses to thus: " property[generate-id()=generate-id(key("cityNames", city)[1])] " which says that this will find the first and only the first (therefore unique) of each city within the key. Creating a group of unique values of all the city's within. Which given the XML above will return "Argyle Broxton Cutler Fenmore Padua Stratmore Oseola" (note that there is no multiples).

Now, my question is thus: why does the second statement return a range of values, instead of just one?

Thanks

A: 

When you define your key, the match expression can match multiple nodes. That node-set is returned when accessing the key by name.

Adding the predicate filter for the first one ensures that you will only get at most one(the first) node returned from the key.

Mads Hansen
I edited my question for better understanding, please have a look at it again and let me know what you think. Thanks.
Gijera
A: 

Ok, I suppose the answer I was looking for is thus:

property[generate-id()=generate-id(key("cityNames", city)[1])] This code finds the first of each city

property[generate-id()=generate-id(key("cityNames", city[1]))] and this code finds the first of all city's

easy enough, just couldn't see it before.

Gijera
Your second XPath sure does not make much sense, and you should not use it. To find the first city (or, more specifically, the city of the first property), use `/listings/property[1]/city`.
Tomalak