XSLT - 按索引访问密钥 - 例如,在Muenchian Grouping中

时间:2009-12-12 04:50:02

标签: xslt key muenchian-grouping

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

在我的教科书(Patrick Carey的XML第二版)中,它提供了一个使用“Muenchian分组”来查找唯一选择的示例。我不明白的部分是:

它到达这里,在示例的进展中,它说:“ property [generate-id()= generate-id(key(“cityNames”,“Cutler”)[1])] “由于'[1]'的索引,这会在选择中找到第一个'Cutler'。鉴于上面的XML将返回”Cutler“

现在这个例子进展到:“ property [generate-id()= generate-id(key(“cityNames”, city )[1])] “这将说明这将找到钥匙内每个城市的第一个也是唯一的第一个(因此是唯一的)。创造一组内部所有城市的独特价值。鉴于上述XML将返回”Argyle Broxton Cutler Fenmore Padua Stratmore Oseola“(请注意,没有倍数)

现在,我的问题是:为什么第二个语句会返回一系列值,而不只是一个?

由于

2 个答案:

答案 0 :(得分:0)

定义密钥时,匹配表达式可以匹配多个节点。按名称访问密钥时返回该节点集。

为第一个添加谓词过滤器可确保您最多只能获得从该键返回的一个(第一个)节点。

答案 1 :(得分:0)

好吧,我想我正在寻找的答案是:

property [generate-id()= generate-id(key(“cityNames”,city)[1])] 此代码查找每个城市的第一个

property [generate-id()= generate-id(key(“cityNames”,city [1]))] 这段代码找到了所有城市的第一个

很容易,以前看不到它。