Home > Back-end >  Using regex to get rid off namespaces xml prefixes with javascript
Using regex to get rid off namespaces xml prefixes with javascript

Time:07-09

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

CodePudding user response:

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"));

CodePudding user response:

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, "</")
)

  • Related