Re: [xsl] Replacing images with alt tags - PART 2

Subject: Re: [xsl] Replacing images with alt tags - PART 2
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Thu, 18 Apr 2002 13:17:57 -0400
[Jacob P. Glenn]

> So, even with breaking up the template the only one that works is the
> "img" template.  If an image is within a <p> tag it still uses the img
> match thus putting <p> tags within <p> tags which is invalid wml.  In
> terms of images as the link of an <a> tag those are not converted.
> Whats going on?  I am new to this but I thought that the XSL was right.
>
> <xsl:template match="a/img">
> <!-- this should replace images with alt text only when img is the
> link-->
>    <xsl:value-of select="@alt"/>
>  </xsl:template>
>
>  <xsl:template match="p/img">
> <!-- replace images with alt text only when img is within a <p>-->
>    <xsl:value-of select="@alt"/>
>  </xsl:template>
>
>  <xsl:template match="img">
> <!-- replace images with alt text in <p> tags when its not within
> tags-->
>    <p><xsl:value-of select="@alt"/></p>
>  </xsl:template>
>

The way you are doing it will produce different results depending on the
context in which you call apply-templates.  If you are within a <p> element,
for example, no p/img is likely to be found, but img will.  This behavior is
what you report.

I don't know just how you are trying to apply your templates, but I'd
suggest a more direct approach.  Specify what you really mean, which is to
act differently depending on who your ancestor is.

For example, the following does what you asked for on a little xml file I
made up for testing;

===========================================
<xsl:template match='img[name(ancestor::*[1])="p"]'>
<xsl:value-of select="@alt"/>
</xsl:template>

<xsl:template match='img[name(ancestor::*[1])="a"]'>
<xsl:value-of select="@alt"/>
</xsl:template>

<xsl:template match='img'>
<p><xsl:value-of select="@alt"/></p>
</xsl:template>
============================================

Or you could be more succinctly by writing

<xsl:template match='img[ancestor::p]'>

although this doesn't say quite the same thing.  The first form specifies
that the first ancestor is a "p" element, the second says that the image has
a "p" element somewhere among its ancestors.  You can decide which form best
expresses what you want to accomplish.

Cheers,

Tom P




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread