[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Replacing images with alt tags - PART 2

Subject: Re: Replacing images with alt tags - PART 2
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Thu, 18 Apr 2002 13:17:57 -0400
alt p
[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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.