This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Search and Replace in XSLT
- To: "ciaran byrne" <ciaran dot byrne at 3tl dot com>
- Subject: Re: Search and Replace in XSLT
- From: Jeni Tennison <jeni dot tennison at epistemics dot co dot uk>
- Date: Fri, 11 Aug 2000 12:03:12 +0100
- Cc: xsl-list at mulberrytech dot com
- Reply-To: xsl-list at mulberrytech dot com
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">
><tag/<
>
>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@epistemics.co.uk
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list