We help IT Professionals succeed at work.
Get Started

Use XSL to rename all occurances of an attribute value

351 Views
Last Modified: 2013-11-19
I need to rename the contents of an attribute wherever it appears within an XML document.   The XML document needs to remain unchanged apart from the rename operation and really this needs to be done as XSL.

So, given the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
 <Expr>
  <var varname="fred"/> := (<var varname="john"/> + <var varname="bill"/>) *<var varname="steve"/>
 </Expr>
 <Object>
  <Code>
   <Expr>
    <var varname="simon"/> := (<var varname="john"/> + <var varname="bill"/>) *<var varname="steve"/>
   </Expr>
  </Code>
 </Object>
</Data>

I need to be able to change all occurances of <var> nodes whose <varname> is "simon" to have a <varname> of "henry", all "bill" to "tony" etc etc

However, I'm getting stuck with the XSL I can use - there could be hundreds of replacements required per document and these will be run against XML docs that are several megabytes in size and I just can't figure out how to code it up.

Other solutions we've come up with are to use the DOM to do the replacements in code (selecting all <var> nodes and checking each one) or do a simple search/replace on the xml document that matches
  varname="simon"
and replaces it with
 varname="henry"

However, we have other XSL to run on the documents and so it seems best to try to get all the transformations done in one place.

Maximum points available as I need an answer quickly and I've not found anything to help either here or on google, so I'm assuming this isn't normally done, either because it can't be, or its too hard to get right.

Thanks for any help.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE