Search

Hi,
I have a datasource generating xml like this:

<shop>
    <prov>
        <item id="1710">blabla</item>
    </prov>
</shop>

Then I have another datasource like this:

<prov-list>
    <initials id="1710">SV</initials>
</prov-list>

I tried to do

<xsl:value-of select="/data/prov-list/initials[@id = prov/item/@id]">

(the context being shop)

Still I manage to do

<xsl:value-of select="/data/prov-list/initials[@id = 1719]" /> <!-- renders SV -->
<xsl:value-of select="prov/item/@id" /> <!-- renders 1719 -->

Thanks!

You're accessing wrong context. When you apply the cross-reference xPath, you need current() function to access current context. See Implementation #1 below.

I strongly recommend Implementation #2.

<shop>
   <entry id="123">
      <title>FOO</title>
      <prov>
            <item id="1710">bla-bla</item>
      </prov>
   </entry>
   <entry id="456">
      <title>BAR</title>
      <prov>
            <item id="1522">bla-bla</item>
      </prov>
   </entry>
</shop>

<prov-list>
   <entry id="1522">
      <initials>KD</initials>
   </entry>
   <entry id="1710">
      <initials>SV</initials>
   </entry>
</prov-list>

<!-- Implementation #1: Access current context using `current()`
<ul>
   <xsl:for-each select="/data/shop/entry">
      <li>
            <xsl:value-of select="title"/>
            <xsl:text> - </xsl:text>
            <xsl:value-of select="/data/prov-list/entry[ @id = current()/prov/item/@id ]/initials"/>
      </li>
   </xsl:for-each>
</ul>

<!-- Implementation #2: Use variables for easier understanding
<ul>
   <xsl:for-each select="/data/shop/entry">
      <xsl:variable name="shopEntry" select="."/>
      <xsl:variable name="provEntry" select="/data/prov-list/entry[ @id = $shopEntry/prov/item/@id ]"/>

      <li>
            <xsl:value-of select="$shopEntry/title"/>
            <xsl:text> - </xsl:text>
            <xsl:value-of select="$provEntry/initials"/>
      </li>
   </xsl:for-each>
</ul>

Thanks Vlad, now it works...

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