Subject: Re: [xsl] key with restricting the scope of use attribute From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Fri, 11 May 2001 15:44:12 +0100 |
Hi Dan, Let's put it this way... When you have a key that uses the generated ID of the node that it matches, the key value can only ever get that single node - none of the rest of the use expression matters, aside from as a test on the values that you use in them. In more detail, when you do: <xsl:key name="MyKey1" match="a/b" use="concat(generate-id(),':',@x,':',@y)"/> <xsl:template match="a/b"> <xsl:for-each select="key('MyKey1',concat(generate-id(),':','1:1'))"> MyKey1:@id=<xsl:value-of select="@id"/><br/> </xsl:for-each> ... </xsl:template> In the template you're looking at a particular b element. This b element will be indexed in the key according to its generated ID, its x attribute and its y attribute. So given the b element: <b id="1" x="1" y="1" /> the key will be something like: randomID1:1:1 *Only* this particular b element will have this key because generate-id() produces a unique ID for a node. Thus each key value will only ever access one node. When you select nodes using the key, the evaluation of the generate-id() for the b element is going to be exactly the same as it was for the key. So for the above b element, the call looks like: key('MyKey1', 'randomID1:1:1') The only node this could possibly retrieve is the one identified by that particular unique ID (i.e. the b element above). It won't retrieve anything, though, if the x attribute and y attributes are not both equal to 1, as it cannot match any nodes. So basically, you're either retrieving the node itself or nothing at all - it comes down to a test of whether the node's x and y attributes are both 1 or not. And that means that it's exactly equivalent to doing: <xsl:template match="a/b"> <xsl:if test="@x = 1 and @y = 1"> MyKey1:@id=<xsl:value-of select="@id" /><br /> </xsl:if> </xsl:template> Usually you use generate-id() in a use expression because you want to restrict the nodes that you retrieve from a key to a subtree of the document - you use the unique ID of the parent of the nodes that you're indexing, or one of its ancestors, or feasibly a descendant - something where *lots* of nodes could have the same node related to them. [BTW, if you've got id attributes, it's more efficient to use them rather than generating unique IDs all the time.] I hope that helps explain why we're struggling to understand what you're aiming for with this particular question. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] key with restricting the , David Carlisle | Thread | Re: [xsl] key with restricting the , Dan Diebolt |
RE: [xsl] Incrementing a Variable, Jon Wynacht | Date | Re: [xsl] Incrementing a Variable, Jeni Tennison |
Month |