首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SoapEnvelope的XSLT删除ns1前缀

使用SoapEnvelope的XSLT删除ns1前缀
EN

Stack Overflow用户
提问于 2020-07-04 04:29:21
回答 1查看 46关注 0票数 0

下午好,

我需要使用SoapEnvelope的XSLT删除ns1前缀

我有下面的XML:

代码语言:javascript
复制
<ns1:mt_BuscarOrdemServico_Request xmlns:ns1="http://www.supergasbras.com.br/service/BuscarOrdemServico">
   <ordemServico>
      <codigo>175811</codigo>
      <codigoOrdemServico>7462242</codigoOrdemServico>
   </ordemServico>
</ns1:mt_BuscarOrdemServico_Request>

我需要创建一个soap信封,以便将我的XML文件发送到以下格式的need服务:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:esb="http://servicos.embratec.com.br/esb" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <esb:buscaOrdemServico>
         <ordemServico xmlns:ns1="http://servicos.embratec.com.br/esb">
            <codigo>11</codigo>
            <codigoOrdemServico>74</codigoOrdemServico>
         </ordemServico>
      </esb:buscaOrdemServico>
   </soapenv:Body>
</soapenv:Envelope

我使用了以下XSLT代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
         <soapenv:Header>
         </soapenv:Header>
         <soapenv:Body>
             <esb:buscaOrdemServico>
                <xsl:copy-of select="*"/>
             </esb:buscaOrdemServico>
         </soapenv:Body>
      </soapenv:Envelope>
   </xsl:template>
</xsl:stylesheet>

But with this XSLT code I am getting the following result:
代码语言:javascript
复制
<soapenv:Envelope xmlns:esb="http://servicos.embratec.com.br/esb" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <esb:buscaOrdemServico>
         <ns1:ordemServico xmlns:ns1="http://servicos.embratec.com.br/esb">
            <codigo>11</codigo>
            <codigoOrdemServico>74</codigoOrdemServico>
         </ns1:ordemServico>
      </esb:buscaOrdemServico>
   </soapenv:Body>
</soapenv:Envelope

你能帮我去掉前缀ns1吗?

EN

回答 1

Stack Overflow用户

发布于 2020-07-04 05:30:23

您可以使用以下命令获得在语义上与您想要的结果相同的结果:

XSLT 1.0

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
        <soapenv:Header/>
        <soapenv:Body>
            <esb:buscaOrdemServico>
                <xsl:copy-of select="*/ordemServico"/>
             </esb:buscaOrdemServico>
         </soapenv:Body>
    </soapenv:Envelope>
</xsl:template>

</xsl:stylesheet>

但是,在XSLT 1.0中复制节点时,也会复制该节点作用域中的名称空间。因此,结果将包含冗余的名称空间声明:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
  <soapenv:Header/>
  <soapenv:Body>
    <esb:buscaOrdemServico>
      <ordemServico xmlns:ns1="http://www.supergasbras.com.br/service/BuscarOrdemServico">
        <codigo>175811</codigo>
        <codigoOrdemServico>7462242</codigoOrdemServico>
      </ordemServico>
    </esb:buscaOrdemServico>
  </soapenv:Body>
</soapenv:Envelope>

这应该不会对接收应用程序造成问题。如果是这样,您可以通过避免完全复制来删除它-例如:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
        <soapenv:Header/>
        <soapenv:Body>
            <esb:buscaOrdemServico>
                <ordemServico>
                    <codigo>
                        <xsl:value-of select="*/ordemServico/codigo"/>
                    </codigo>
                    <codigoOrdemServico>
                        <xsl:value-of select="*/ordemServico/codigoOrdemServico"/>
                    </codigoOrdemServico>
                </ordemServico>
             </esb:buscaOrdemServico>
         </soapenv:Body>
    </soapenv:Envelope>
</xsl:template>

</xsl:stylesheet>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62722305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档