share [fb] share [tw]

I have XML like

<A>
 <B>
  <C>
   Hello World
  </C>
 </B>
</A>

I want to replace the starting tag's "<" with "<ns:" in each tag resulting the following XML

<ns:A>
 <ns:B>
  <ns:C>
   Hello World
  </ns:C>
 </ns:B>
</ns:A>

What should be the regular expression I should use in text editor to replace and include the namesapace

I was trying to use regex [<][^/] but it selects the 1st character of the starting tag also which I don't want to replace.

Note: I have the above requirement for manual editing purpose in an editor where regex replace is supported. I was not going to do the above task programmatically .And the output XML fragment requested is an inner part of the complete XML , hence namespace URI is not mentioned

link|improve this question
1  
Don't crosspost please, thank you. Regex to match starting node of XML without the tag name – slhck Nov 2 '11 at 9:12
I don't get it is superuser.com and stackoverflow are different site or same ? If same why do I need to create another login and if different why can't I post in both ? – NeilGhosh Nov 2 '11 at 9:26
They are different sites, but posting the same question to multiple sites is not encouraged. You already found your answer on Stack Overflow, so there's no need to post it here as well. See: meta.stackoverflow.com/questions/64068/… – slhck Nov 2 '11 at 10:14
Ok got it, Actually when I posted it in superuser.com I have not got the answer in stackoverflow by that time. Anyway going forward I will post only in Stack Overflow.Thanks. – NeilGhosh Nov 2 '11 at 10:26
Thanks for your consideration! – slhck Nov 2 '11 at 10:28
feedback

closed as off topic by slhck, Sathya Nov 2 '11 at 14:12

Questions on Super User are expected to generally relate to computer software or computer hardware, within the scope defined in the faq.

1 Answer

Never attempt to manipulate XML with regular expressions. Use proper XML processing tools such as XSLT. This task is very easy to do with XSLT: it's a single template rule

<xsl:template match="*" xmlns:ns1="zzzzzzzz">
  <xsl:element name="ns1:{local-name()}>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

I'm assuming of course that you want to add a namespace declaration for the prefix ns1. If you want to create badly-formed output in which the namespace isn't declared, then XSLT won't let you do that - which is of course why you should be using it.

link|improve this answer
Thanks Michael, The above requirement was for manual editing purpose.for programmatic transformation I would have used XML processing tools. I have edited the question and added the same in the note at the bottom of the question. – NeilGhosh Nov 2 '11 at 11:15
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.