samash
samash

Reputation: 777

Regular expression to add namespace prefix in xml

I am stuck at one point.I have a xml which does not have a prefix. I am trying to put the prefix through Regular expression. My xml looks as below:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tier="http://xxxx">
<soapenv:Header/>
<soapenv:Body>
  <tier:UnisysMB_Dispatch>
     <PayLoad>
    <XMLTransaction>
        <Source_Identifier>WFM.MSM.SR</Source_Identifier>
        </XMLTransaction></PayLoad>
  </tier:UnisysMB_Dispatch>
</soapenv:Body>
</soapenv:Envelope>

but I want the xml with namespace prefix with every tag (s1) like below:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:tier="http://xxxx" s1="someurl">
<soapenv:Header/>
<soapenv:Body>
  <tier:UnisysMB_Dispatch>
     <s1:PayLoad>
    <s1:XMLTransaction>
        <s1:Source_Identifier>WFM.MSM.SR</s1:Source_Identifier>
        <s1:/XMLTransaction></s1:PayLoad>
  </tier:UnisysMB_Dispatch>
</soapenv:Body>
</soapenv:Envelope>

I have tried this search regex ="(<\/?)[a-z0-9]+"; replace =s1:"$1" but not working..

any help please??

Upvotes: 1

Views: 1670

Answers (1)

CoronA
CoronA

Reputation: 8095

Replace (</?)(\w+)> with $1s1:$2>. Now this should only work fine in your IDE.

For the first line you have to use another regex. (<soapenv:Envelope.*?)> by $1 s1="someUrl">.

I agree with other comments, that regex parsing is not the best way to extract information from XML. Actually this is not parsing, but replacing. This should work for small examples.

If you are in need for a robust solution that works for a variable number of WSDLs, you should switch to an XML parser/rewriter. I think that some XML-Tools support your problem out of the box.

Upvotes: 1

Related Questions