0

I'm trying to get the value of iWantToGetThis.jpg and put it into an <img> during my XSL transformation. This is how my xml is structures:

<article>
    <info>
        <mediaobject>
            <imageobject>
                <imagedata fileref='iWantToGetThis.jpg'>

Here's what I've come up with for the XSL:

<xsl:template name="user.header.content">
    <xsl:stylesheet xmlns:d='http://docbook.org/ns/docbook'>
        <img><xsl:attribute name="src">../icons/<xsl:value-of select='ancestor-or-self::d:article/info/mediaobject/imageobject/imagedata/@fileref' /></xsl:attribute></img>
    </xsl:stylesheet>
</xsl:template>

The image is being added to the output, but the src attribute is set to "../icons/", so I'm assuming it's not finding the fileref attribute in the XML. This looks perfectly valid to me, so I'm not sure what I'm missing.

Scribblemacher
  • 1,518
  • 1
  • 16
  • 31

1 Answers1

0

I am not sure how you can get anything back at all, because that does not look like a valid XSLT document (I would expect the error "Keyword xsl:stylesheet may not contain img.").

However, it may be you are just showing a fragment of the code. If this is the case, your issue may be that you have only specified the namespace for the article element, when you really need to specify it for all elements in your xpath. Try this

<xsl:value-of 
   select="ancestor-or-self::d:article/d:info/d:mediaobject/d:imageobject/d:imagedata/@fileref"/>

Another possible problem may be because you are using the 'ancestor-or-self' xpath axis to find the attribute. This would only work if your current context was already on the article element, or one of its descendants.

As a side note, you can simplify the code by making use of Attribute Value Templates here

<img src="../icons/{ancestor-or-self::d:article/d:info/d:mediaobject/d:imageobject/d:imagedata/@fileref}" />
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • It was simply the lack of namespaces on children in the xpath expression. – Scribblemacher Jun 01 '12 at 16:46
  • This form of stylesheet is allowed when the full form would consist of just a single `` element. The stylesheet contains just the content of that template element. [Read about it here](http://www.w3.org/TR/xslt#result-element-stylesheet). – Borodin Jun 01 '12 at 17:12
  • The XSL I'm working with is a lot more fleshed out. I realized after posting this that it's not really a good XSL example... – Scribblemacher Jun 01 '12 at 17:23