Re: [xsl] attribute value replacement and good book recommendation.

Subject: Re: [xsl] attribute value replacement and good book recommendation.
From: Abel Braaksma <>
Date: Sat, 08 Sep 2007 11:09:42 +0200
Hi Charlie,

I see you've been away from this list since end 2006, welcome back ;)

Changing attributes is as easy as 1-2-3 in XSLT, if you know how, that is. You asked for a book as well. This template is explained in just about any book on XSLT: in the "XSLT Cookbook" 1st edition and 2nd edition (chapter 8); in Jeni Tennison's introductory book on XSLT; in XSLT 2.0 Programmer's Reference (Michael Kay), under "xsl:copy"; in "XSLT and XPath: A guide to XML Transformations" in chapter 9.2 (but not well enough explained imho) etc etc. All these books are, as far as I know, still available. I personally favor the Cookbook 2nd ed for starters if you are good in working with examples, or Jeni Tennison's "Beginning XSLT" (for xslt 1.0) or "Beginning XSLT 2.0" (for xslt 2.0) if you like to get a good explanation and step by step of the language.

This is what they all explain:

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

or some variant on that theme. It will copy the input XML to the output without change. Next, all you have to do is add templates for the thing you do want to change. For instance, in your case, @username and @password when they are children of 'property'. Just add the following to your stylesheet alongside the copy idiom:

<xsl:template match="property/@username">
<xsl:attribute name="username">
<xsl:value-of select="$user" />

<xsl:template match="property/@password">
<xsl:attribute name="password">
<xsl:value-of select="$pwd" />

Of course, don't forget to include the parameters in your stylesheet with the names you used in your ANT build script. I.e., use the following top level declarations:

<xsl:param name="pwd" />
<xsl:param name="user" />

That's all there is to it. Really. Happy coding!

-- Abel Braaksma

PS: a start about XSLT: (see section on Further reading, it is a bit out of date, but gives you a good starting point).

chun ji wrote:
Hi there, I have couple of questions here.

I want to replace some attribute values in a xml file,
but I am kind new to the XSLT and is blocked for what
I should do next.

So here is my original xml file, old.xml,

 <?xml version="1.0" encoding="UTF-8"?>
xmlns=  >

<bean id="wandDataSource" class=""
destroy-method="close" scope="singleton">
<property name="driverClassName"
<property name="url"
<property name="username"
<property name="password"

I want to replace the username & password with the
value I give, for example, newuser & newpassword.
But that old.xml file is randomly generated, and I
dont know the value of username and password in
it, when these are being replaced. So in that case,
what this xsl template file should be looks like?

Here is the target in my ANT script, that I used to
try to make that replacement.  
<target name="updateXML">
<xslt in="old.xml" out="new.xml"
<param name=user" expression=newuser"/>
<param name="pwd" expression="newpassword "/>

Q2. I started to learn this kind of XML/XSLT tool almost 9
months ago, , but I still feel that there are lots of
things that I dont know. Does someone know if there
are some good XML books in the market? Or some website
that can give a beginner very decent XML education? I
think that would help me a lot.

Thanks in advance.


Pinpoint customers who are looking for what you sell.

Current Thread