Modify XML tag using shell script

Hi All

Need some help with a unix shell script. I have a XML file as shown below:

<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/service/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider>
<urlRewrite>/service/efg/deleteCustomerAccount</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>

My requirement is to look up for each occurence of the XML node "urlRewrite" in the above file, tokenize the value of this "urlRewrite" node based on '/' and then replace the value with "/test/${lastToken}". All other nodes should be copied as is to the output file.

So the output file should look like below:

<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/test/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider> 
<urlRewrite/test/deleteCustomerAccount</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>

Any help on this is much appreciated

Thanks
Ankur

How about this ?

 
perl -pe 's/(\<urlRewrite\>)(.*)\/(.+?)(\<\/urlRewrite\>)/$1\/test\/$3$4/' filename

Thnaks a ton Pravin. Works perfectly. Just one question. I am not much of a Unix scripting guy so need to know if this would work even if the "urlRewrite tag has more than 3 tokens. I mean the value could have n number of '/'. The code should pick the last token and replace it with "/test/${lastToken}".

For example if the value under "urlRewrite is as below:

<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>

Will your code still replace it with :

<urlRewrite>/test/getAccountDetails</urlRewrite>

Thanks Again for your prompt response.

below is sample file, which i have modified (original from post #1)
You can run perl code on this sample file and check result.

<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/service/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider>
<urlRewrite>/service/efg/deleteCustomerAccount</urlRewrite>
<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>
<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>
<urlRewrite>/service/abc/def/xyz//test5/test8/test9/getAccountDetails</urlRewrite>
<urlRewrite>/service//test1/test2/test3/abc/def/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>