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

Re: Search and Replace in XSLT

Subject: Re: Search and Replace in XSLT
From: Jeni Tennison <jeni.tennison@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Aug 2000 12:03:12 +0100
xslt replace tag
Ciaran,

>let's say I have:
>
><img src="x.bmp" alt="[tag]"/>
>
>and I want to replace '[' with '<' to get the output
>
><img src="x.bmp">
><tag/>
>
>using Michaels' technique I get the usual entity problem in XML
>i.e. the output becomes:
>
><img src="x.bmp">
>&lt;tag/&lt;
>
>which is no good to me as I need the proper tag.

You're thinking about generating XML in the wrong way if you want to use
XSLT to do it.  Rather than thinking "I am generating a string, with a '<',
then the string 'tag', then a '/', then a '>', to give an empty element
called 'tag'", you should be thinking "I am generating an empty element
called 'tag'".  [This goes for other things as well as just generating
elements: XSLT processes and produces a *structure*, not a sequence of
characters.]

To generate an element with a variable name (presumably the @alt attribute
could hold anything), you can use the xsl:element element:

<xsl:element name="...">
  ...
</xsl:element>

[For an empty element, <xsl:element name="..." />]

The value of the @name attribute on xsl:element should be an XPath that
selects something with a string value that gives you the name.  Let's say
you're matching on the 'img' element:

<xsl:template match="img">
  ...
</xsl:template>

Within this template, the current node is the 'img' element, so you can get
the value of the @alt attribute through the XPath '@alt':

<xsl:template match="img">
  ...
  <xsl:element name="@alt" />
</xsl:template>

In your case, this would actually give you an element called '[tag]', which
isn't a valid name for an element, so you need to do a bit of
string-munging to get rid of the '[' and ']' first:

<xsl:template match="img">
  ...
  <xsl:element name="string-after(string-before(@alt, ']'), '[')" />
</xsl:template>

I hope that this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



 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.