output html from XML - XSLT

Hi All
I have just got into XSLT basically i am doing a simple HTML transform.  I want to be able to use an image with a few more details than just src.....

my xml.
<area Title="Area1">
<image>image source</image>
</area>
my xslt.

<xsl:template match="area">
    <p class="HelpPages">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

<xsl:template match="image">
    <img>
      <xsl:attribute name="src">
        <xsl:apply-templates/>
      </xsl:attribute>
    </img>
  </xsl:template>

I have other templates for different XML tags.
THIS WORKS.

What i want to do is
<image src="path" width="45" height="45"/>

So how do i access the src. width values?? i have tried....

<xsl:template match="image">
    <img>
      <xsl:attribute name="src">
        <xsl:value-of select="image/@src"/>
      </xsl:attribute>
    </img>
  </xsl:template>
 but with no joy....i have also tried select="@src" and select="area/image/@src".

looking at this link http://www.stylusstudio.com/xsllist/200008/post30330.html its seems pretty straight forward....what am i missing?

thanks
steve

LVL 7
osiris247Asked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
<xsl:template match="image">
    <img>
      <xsl:attribute name="src">
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:attribute name="width">45</xsl:attribute>
      <xsl:attribute name="height">45</xsl:attribute>
    </img>
  </xsl:template>

The value you want to use as src is the text of the node rather than an attribute and, since Image is the context node in your template, you just use . (dot) to get its value.
0
 
osiris247Author Commented:
thank Carl

but what i want is to use the attribute so i can specify different widths etc. (good to know about the dot thing though)

I want my XML to look like this...
<image src="path" width="45" height="45"/>


Steve
0
 
Carl TawnSystems and Integration DeveloperCommented:
Oh, sorry, misunderstood.

If your XML looks like:   <image src="path" width="45" height="45"/>

Then the "image" template in your XSLT becomes:

    <xsl:template match="image">
        <img>
          <xsl:attribute name="src">
            <xsl:value-of select="."/>
          </xsl:attribute>
          <xsl:attribute name="width"><xsl:value-of select="@width" /></xsl:attribute>
          <xsl:attribute name="height"><xsl:value-of select="@height" /></xsl:attribute>
       </img>
     </xsl:template>
0
 
osiris247Author Commented:
Excellent thanks carl....

except i had to change
<xsl:attribute name="src">
            <xsl:value-of select="."/>
</xsl:attribute>
to..
<xsl:attribute name="src">
            <xsl:value-of select="@src"/>
</xsl:attribute>

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.