Re: [xsl] Replace an attribute

Subject: Re: [xsl] Replace an attribute
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 4 Jan 2008 10:45:09 +0000
On 04/01/2008, seantiley@xxxxxxxxxxx <seantiley@xxxxxxxxxxx> wrote:
>
> Hi there,
> I am new to xslt but I have an xml file in the following format
> <root>
>     <file name=3D"c:\blah\blah\class.java">
>         <error line=3D"1" message=3D"Parameter status should be final"/>
>     </file>
> </root>
>
> I would like to be able to replace the name attribute of the file =
> element in the original file and leave the rest of the file intact, then =
> pass the results to a different stylesheet.
>
> The first step is simply replacing the attribute
>
> I had a look at the replace function and tried something like this
> <xsl:value-of select=3D'replace(@name,"c:\blah\blah\*","files")'/>
>
> Unfortunately that seemed to have no effect at all.
>
> I must be missing something painfully obvious

When you basically want to make a copy of the input XML but with
certain changes then you want to use the "indentity" transform, with
specific templates to make the change.

You're right to use the replace() function, but remember to get the
regex right.

<xsl:stylesheet
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
    exclude-result-prefixes="xs">

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@name">
	<xsl:attribute name="name">
		<xsl:value-of select="replace(., 'C:\\blah\\blah', 'files')"/>
	</xsl:attribute>	
</xsl:template>

</xsl:stylesheet>


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread