Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Using regex to get rid off namespaces xml prefixes with javascript

I have a soap and I need remove all namespaces prefix from text.

In the next sample the prefixes are "s" and "ans", but they can change all time. The soap xml is something as:

xmlStr=`    <s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas">
        <s:Body>
            <ans:respostaElegibilidadeWS>
                <ans:cabecalho>
                    <ans:identificacaoTransacao>
                        <ans:tipoTransacao>SITUACAO_ELEGIBILIDADE</ans:tipoTransacao>
                    </ans:identificacaoTransacao>
                </ans:cabecalho>
            </ans:respostaElegibilidadeWS>
        </s:Body>
    </s:Envelope>`

I need:

<Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas">
    <Body>
        <respostaElegibilidadeWS>
            <cabecalho>
                <identificacaoTransacao>
                    <tipoTransacao>SITUACAO_ELEGIBILIDADE</tipoTransacao>
                </identificacaoTransacao>
            </cabecalho>
        </respostaElegibilidadeWS>
    </Body>
</Envelope>

I have tried:

xmlStr = xmlStr.replace(/<(\/?)\w+:(\w+\/?) ?(\w+:\w+.*)?>/g, "$1$3");

But no look

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

For the example data, you might use 2 capture groups and match the word characters followed by a colon.

In the replacement use the 2 capture groups.

(<\/?)\w+:(\w+[^>]*>)

Regex demo

xmlStr=`    <s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas">
        <s:Body>
            <ans:respostaElegibilidadeWS>
                <ans:cabecalho>
                    <ans:identificacaoTransacao>
                        <ans:tipoTransacao>SITUACAO_ELEGIBILIDADE</ans:tipoTransacao>
                    </ans:identificacaoTransacao>
                </ans:cabecalho>
            </ans:respostaElegibilidadeWS>
        </s:Body>
    </s:Envelope>`;
console.log(xmlStr.replace(/(<\/?)\w+:(\w+[^>]*>)/g, "$1$2"));

about 4 years ago · Juan Pablo Isaza Report

0

Just make 2 regular expressions, it's easy to replace them this way.

var xmlStr = `    <s:Envelope
        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas">
        <s:Body>
            <ans:respostaElegibilidadeWS>
                <ans:cabecalho>
                    <ans:identificacaoTransacao>
                        <ans:tipoTransacao>SITUACAO_ELEGIBILIDADE</ans:tipoTransacao>
                    </ans:identificacaoTransacao>
                </ans:cabecalho>
            </ans:respostaElegibilidadeWS>
        </s:Body>
    </s:Envelope>`;

var reg1 = /<\w+:/g
var reg2 = /<\/\w+:/g

console.log(xmlStr
  .replace(reg1, "<")
  .replace(reg2, "</")
)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!