Search

Hi All,

I'm still learning, so this may be a trivial questoin. Yet, the answer isn't easy to find.

I want to use a variable inside a template match attribute, but this gives me an error. I'm not sure how else to achieve this:

  <aanleveren-detail>
    <pagination total-entries="1" total-pages="1" entries-per-page="20" current-page="1" />
    <section id="20" handle="aanleveren">Aanleveren</section>
    <entry id="49">
      <talen>
        <item name="nl" handle="nl" id="48">
          <title handle="ftp-inloggen">FTP Inloggen</title>
          <omschrijving-kort mode="formatted"><p>Dit is een omschrijving in het NL</p>
</omschrijving-kort>
          <stappen field-id="68" subsection-id="19" items="2">
            <item id="47">
              <title handle="stap-1">Stap 1</title>
              <omschrijving mode="formatted"><p>Met een omschrijving.</p>
</omschrijving>
            </item>
            <item id="50">
              <title handle="stap-2">Stap 2</title>
              <omschrijving mode="formatted"><p>Met een omschrijving.</p>
</omschrijving>
            </item>
          </stappen>
        </item>
        <item name="en" handle="en" id="53">
          <title handle="english-title">English Title</title>
          <omschrijving-kort mode="formatted"><p>With short description.</p>
</omschrijving-kort>
          <stappen field-id="68" subsection-id="19" items="2">
            <item id="51">
              <title handle="step-1">Step 1</title>
              <omschrijving mode="formatted">With description.</omschrijving>
            </item>
            <item id="52">
              <title handle="step-2">Step 2</title>
              <omschrijving mode="formatted">With description.</omschrijving>
            </item>
          </stappen>
        </item>
      </talen>
      <system-date>
        <created iso="2013-01-24T09:20:41+01:00" time="09:20" weekday="4" offset="+0100">2013-01-24</created>
        <modified iso="2013-01-24T09:22:03+01:00" time="09:22" weekday="4" offset="+0100">2013-01-24</modified>
      </system-date>
    </entry>
  </aanleveren-detail>

With this xslt:

<xsl:template match="data">
    <h1><xsl:value-of select="$page-title"/></h1>
    <xsl:apply-templates select="aanleveren-detail/entry" />
</xsl:template>

<xsl:template match="aanleveren-detail/entry">
    <h2><xsl:value-of select="talen/item[@handle = $current-language]/title" /></h2>
    <xsl:value-of select="talen/item[@handle = $current-language]/omschrijving-kort" disable-output-escaping="yes" />
    <xsl:apply-templates match="talen/item[@handle = $current-language]/stappen/item" />
</xsl:template>

<xsl:template match="talen/item[@handle=$current-language]/stappen/item">
    <h4><xsl:value-of select="title" /></h4>
</xsl:template>

Gives me the following error: XSLTProcessor::importStylesheet(): Undefined variable Failed to compile predicate

The $current-language variable is working fine, but can't be processed when it's used in the match attribute? How can I overcome this problem?

EDIT I have used this now:

SELECTIE MENU

    <h4>ARTIKELEN</h4>
    <xsl:for-each select="talen/item[@handle = $current-language]/stappen/item">
        <h4><xsl:value-of select="title" /></h4>
        <xsl:value-of select="omschrijving" disable-output-escaping="yes" />
    </xsl:for-each>
</xsl:template>

But i'm still wondering about the how and why about my question.

First look through:

<xsl:apply-templates match="talen/item[@handle = $current-language]/stappen/item" />

The match attribute is incorrect. It should say select.

And change:

<xsl:template match="talen/item[@handle=$current-language]/stappen/item">
    <h4><xsl:value-of select="title" /></h4>
</xsl:template>

So that it does not include the predicate:

<xsl:template match="item">
    <h4><xsl:value-of select="title" /></h4>
</xsl:template>    

Variables cannot be used in the value of a match attribute.

So, you have two errors here. A typo and your use of a variable in the match attribute. You don't need the predicate in both places anyways, in this case it must and is only necessary to have it in the select attribute of the <xsl:apply-templates /> element.

Hope that helps!

Ah, that's great Lewis. Awesome response! Stupid I have completely overlooked the match in the apply-templates tag :S.

And about the second error. Ok, this makes me understand the selections process a little better. The template goes for any item, and I can apply it contextually, I think ;).

If you're worried about matching unexpectedly then you have the mode attribute for templates to help. Also remember that match attributes work on a specificity basis in a very similar way to css selectors.

Stupid I have completely overlooked the match in the apply-templates tag :S.

It happens... but if it becomes routine we will commence flogging with wet noodles :)

And about the second error. Ok, this makes me understand the selections process a little better. The template goes for any item, and I can apply it contextually, I think ;).

Correct. Check out the mode and priority attributes to learn how you can get fancy.

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