Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda