Problem with predicates
This is an open discussion with 5 replies, filed under XSLT.
Search
Maybe <xsl:apply-templates select="/data/offices/entry[region/item/@handle = current()/@handle]" /> ?
Update.
For some reason while
<xsl:for-each select="regions/entry/region"> <h2 id="{@handle}"><xsl:value-of select="." /></h2> <xsl:apply-templates select="/data/offices/entry[region/item/@handle = @handle]" /> </xsl:for-each>
doesn't do a thing, using a variable instead of @handle seems to work.
<xsl:for-each select="regions/entry/region"> <xsl:variable name="currentregion" select="@handle"/> <h2 id="{@handle}"><xsl:value-of select="." /></h2> <xsl:apply-templates select="/data/offices/entry[region/item/@handle = $currentregion]" /> </xsl:for-each>
but why??? Shouldn't using @handle just work?
@andrrr Yeah that works too! Thanks :)
I'd love to know why you need this 'current()'.
I'd love to know why you need this 'current()'.
Because inside a predicate your context is being switched to /data/offices/entry
/data/offices/entry[region/item/@handle = @handle]
compares /data/offices/entry/region/item/@handle
to /data/offices/entry/@handle
.
By using current()
you're switching back to the context from wich you were executing apply-templates
(the node currently selected by for-each
).
Aaaaahhhh! Makes perfect sense. Thanks phoque.
Create an account or sign in to comment.
Ok, this is a follow on from this post
I have the following XML
and I'm trying to build a list of products sorted by region from it with the following template
But for some reason, returns nothing.
I have tried debugging by including
and also
the first one outputs nothing, the second one outputs the result of the first node encountered for every single instance of 'region' so I'm at a total loss.
Thanks for any help you can provide with this.