0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, It didn't work. I get <s:Envelope into result
@LuizAlves I have updated it
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.