Altova Mailing List Archives


Re: xsl:key context

From: "Dimitre Novatchev" <dimitren@---.---.-->
To: NULL
Date: 1/16/2007 8:16:00 PM


<deja@2...> wrote in message 
news:1168970919.666572.159130@v......
> i've got an rtf in a variable $validCars like this:
>
> <Car name="Ford">
> <Door name="myDoor">
> </Door>
> </Car>
> <Car name="Toyota">
> <Door name="yourDoor">
> </Door>
> </Car>
>
> also got a document like this
> <Vehicles>
>  <Car>
>     <Name>Ford</Name>
>     <Doors>
>       <DoorName>myDoor</DoorName>
>       <DoorName>yourDoor</DoorName>
>     </Doors>
>   </Car>
>  <Car>
>     <Name>Jaguar</Name>
>     <Doors>
>       <DoorName>myDoor</DoorName>
>       <DoorName>yourDoor</DoorName>
>     </Doors>
>   </Car>
> </Vehicles>
>
> I want to select cars which have a valid DoorName (i.e their text
> appears as an attribute in my variable as a child of the same car which
> also must be valid). so in the above case it will select the Ford node
> only.
>
> I tried this but it doesn't work
>
> <xsl:key name="recognisedCar" match="/Car" use="@name"/>
>
> <xsl:for-each select="msxsl:node-set($validCars)">
>  <xsl:for-each
> select="$document/Vehicles/Car[Doors/DoorName=key('recognisedCar',Name)/Door/@name])">
>
>  <xsl:value-of select="Name"/>
> </xsl:for-each>
>
> </xsl:for-each>
>
> I assume it is something to do with the context of the key changing.
> Any ideas?


Yes, it has to do a lot with the current document. Here's one possible 
solution (do note that no extension function was necessary):

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:key name="kCarByNameAndDoor" match="Car"
  use="concat(@name, '+', Door/@name)"/>

 <xsl:variable name="vrtfValidCars">
  <cars>
  <Car name="Ford">
   <Door name="myDoor">
   </Door>
  </Car>
  <Car name="Toyota">
   <Door name="yourDoor">
   </Door>
  </Car>
  </cars>
 </xsl:variable>

 <xsl:template match="/">
   <xsl:for-each select="/*/Car/*/DoorName">
     <xsl:variable name="vCar" select="../.."/>
     <xsl:variable name="vKey" select=
       "concat($vCar/Name, '+', .)"/>
     <xsl:for-each select="document('')">
       <xsl:if test="key('kCarByNameAndDoor', $vKey)">
         <xsl:value-of select="concat($vCar/Name, '&#xA;')"/>
       </xsl:if>
     </xsl:for-each>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied on the xml document provided:

<Vehicles>
 <Car>
  <Name>Ford</Name>
  <Doors>
   <DoorName>myDoor</DoorName>
   <DoorName>yourDoor</DoorName>
  </Doors>
 </Car>
 <Car>
  <Name>Jaguar</Name>
  <Doors>
   <DoorName>myDoor</DoorName>
   <DoorName>yourDoor</DoorName>
  </Doors>
 </Car>
</Vehicles>

produces the wanted result:

Ford

Hope this helped.

Cheers,
Dimitre Novatchev 


Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.