0

I have a XML posted to a WebService and I am having problems because it expects other xml format. I need transform the next xml adding xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas and removing the namespace from solicitacaoProcedimentoWS node.

Some help will be appreciated. Thanks in advance, Luiz

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <solicitacaoProcedimentoWS xmlns="http://www.ans.gov.br/padroes/tiss/schemas">
      <cabecalho>
        <identificacaoTransacao>
          <tipoTransacao>SOLICITACAO_PROCEDIMENTOS</tipoTransacao>
          <sequencialTransacao>k1</sequencialTransacao>
        </identificacaoTransacao>
      </cabecalho>
      <hash>393d3f1e310f3385ad0398dd9b65dc4a</hash>
    </solicitacaoProcedimentoWS>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I´d like to transform to:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SOAP-ENV:Header/>
      <SOAP-ENV:Body>
        <ans:solicitacaoProcedimentoWS>
          <ans:cabecalho>
            <ans:identificacaoTransacao>
              <ans:tipoTransacao>SOLICITACAO_PROCEDIMENTOS</ans:tipoTransacao>
              <ans:sequencialTransacao>k1</ans:sequencialTransacao>
            </ans:identificacaoTransacao>
          </ans:cabecalho>
          <ans:hash>393d3f1e310f3385ad0398dd9b65dc4a</ans:hash>
        </ans:solicitacaoProcedimentoWS>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
2
  • What's the difference - other than cosmetics - between the input and the output? Commented Feb 11, 2015 at 12:08
  • Hi, There is not difference, but the webservice doesn´t accept if I don´t transform the xml format. I can´t change the webservice beacuse is not mine. Commented Feb 11, 2015 at 13:28

1 Answer 1

1

The following XSLT 2.0 stylesheet should do what you require:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas">

  <!-- identity transformation - keep everything the same except when overridden -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>

  <!-- add an extra NS declaration to the document element -->
  <xsl:template match="/*">
    <xsl:copy>
      <xsl:namespace name="ans" select="'http://www.ans.gov.br/padroes/tiss/schemas'"/>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <!-- use a prefixed name for any element in the namespace
       http://www.ans.gov.br/padroes/tiss/schemas -->
  <xsl:template match="ans:*">
    <xsl:element name="ans:{local-name()}" namespace="http://www.ans.gov.br/padroes/tiss/schemas">
      <xsl:apply-templates select="@*|node()" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

But the real problem is with the service you're talking to - as you're aware, both versions of the file have all the same elements in all the same namespaces, so should be treated the same by any namespace-aware XML processor. If they care about the use of specific prefixes in this way then they're probably not using a proper namespace-aware XML parser, and who knows what other aspects of the XML specifications they're ignoring or abusing. If they require the body elements to be named with an ans: prefix, does that mean they also require the envelope to use the SOAP-ENV: prefix? At least one SOAP toolkit I've used in the past uses soap: for this namespace instead of SOAP-ENV:...


If you're limited to XSLT 1.0 then you can't use <xsl:namespace> to create arbitrary namespace bindings, instead try something like

<xsl:copy-of select="document('')/*/namespace::ans" />

to copy the namespace declaration from the stylesheet document itself.

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

3 Comments

Thank you very much. Your xslt works great. The body content prefix is required but the envelope prefix is free.
@LuizAlves glad to help. On Stack Overflow, when you get an answer that solves your problem it's polite to mark it accepted (by clicking the tick mark to the left)
Sorry, Mr Ian Roberts. I just did it. I am a newbie on Stack Overflow.

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.