Subject: value-of inside template match
Posted By: RoeZ Post Date: 4/10/2008 2:01:39 PM
I am having this really noobish problem wich i can't find the simple answer for :(

I have a translation in an xml document wich I put in the select of a apply-templates, this translation has an xml element in it called 'link'

Now i have a xsl:template match="link" wich get's called like it should.
In this template i select something from the root source with xsl:value-of. The value i select does exist, i checked. Though this value doesn't get applied in the template. I kinda know it must be a easy problem but i think it's too general to search for, i looked at numerous examples and they all do it like this.

Reply By: bonekrusher Reply Date: 4/10/2008 4:27:30 PM
Hi,

Please post a sample XML file, the psrt of the XSLT that you are having trouble with and desired output.

Regards,


Reply By: mhkay Reply Date: 4/10/2008 5:04:27 PM
There's a bug in your code.

If you show us your code, and its input, then we may be able to help you find it.

Do you really imagine we can find the bug without seeing the code?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: RoeZ Reply Date: 4/13/2008 10:05:20 AM
well, i have this xml file which has the following node inside:


<intro>bla bla bla <link><url>gmail</url><text>Gmail</text></link> bla bla bla</intro>


i read in this file with the following xsl:


<xsl:variable name="xml.email" select="document('Translations/Email.xml')"/>
<xsl:variable name="lang.email" select="$xml.email/translations/info[lang($language)]" />


i call this 'text' with the following xsl:


<xsl:apply-templates select="$lang.email/intro" />


then i have this template which gets aplied:


<xsl:template match="link">
    <a>
        <xsl:attribute name="href">
            /email/<xsl:value-of select="url"/>/<xsl:value-of select="/source/pictureId"/>/<xsl:value-of select="/source/titleId"/>
        </xsl:attribute>
        <xsl:value-of select="text"/>
    </a>
</xsl:template>


the template does get called, but the 2 values inside (/source/pictureId and /source/titleId) aren't inserted, they are in the input xml though, i know this because when i do this, i get the right value from /source/pictureId:


<xsl:value-of select="/source/pictureId"/>
<xsl:apply-templates select="$lang.email/intro" />


so when i do the above, i see the value of /source/pictureId in front of my bla bla text.

Sorry for not posting the code, i didn't thought i had a bug but a silly beginner mistake

Reply By: Martin Honnen Reply Date: 4/13/2008 10:11:58 AM
So you have two XML input documents, the primary contaning a 'source' element, and a secondary you pull in with the document function. Use a global variable to store the root of the primary input document e.g.

<xsl:variable name="main-root" select="/"/>

then use that variable in your template:

<xsl:template match="link">
    <a>
        <xsl:attribute name="href">
            /email/<xsl:value-of select="url"/>/<xsl:value-of select="$main-root/source/pictureId"/>/<xsl:value-of select="$main-root/source/titleId"/>
        </xsl:attribute>
        <xsl:value-of select="text"/>
    </a>
</xsl:template>


--
  Martin Honnen
  Microsoft MVP - XML
Reply By: RoeZ Reply Date: 4/17/2008 12:09:45 PM
Thank you very much, this works like a charm!


Go to topic 70686

Return to index page 1