Search

Ok, this is a follow on from this post

I have the following XML

<products>
    <section id="10" handle="products">Products</section>
    <entry id="42">
        <associated-office>
            <item id="43" handle="bolton-office" section-handle="offices" section-name="Offices">Bolton office</item>
        </associated-office>
        <name handle="pastures">Pastures</name>
    </entry>
</products>
<offices>
    <section id="9" handle="offices">Offices</section>
    <entry id="46">
        <office-name handle="bolton-office">Bolton office</office-name>
        <county handle="lancashire">Lancashire</county>
        <region>
            <item handle="north-west">North-West</item>
        </region>
    </entry>
</offices>
<regions>
    <section id="14" handle="regions">Regions</section>
    <entry id="78">
        <region handle="north-west">North-West</region>
    </entry>
</region>

and I'm trying to build a list of products sorted by region from it with the following template

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

<xsl:template match="offices/entry">
    <xsl:value-of select="office-name" />
    <xsl:apply-templates select="/data/products/entry[associated-office/item/@handle = office-name/@handle]" />
</xsl:template>

<xsl:template match="products/entry">
    <xsl:value-of select="name" />
</xsl:template>

But for some reason, returns nothing.

I have tried debugging by including

<xsl:value-of select="/data/offices/entry[region/item/@handle = @handle]" />

and also

<xsl:value-of select="/data/offices/entry[region/item[@handle = @handle]]" />

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.

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.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details