【问题标题】:Remove ns1 prefix with XSLT for SoapEnvelope使用 XSLT 为 SoapEnvelope 删除 ns1 前缀
【发布时间】:2020-07-03 20:29:20
【问题描述】:

下午好,

我需要为 SoapEnvelope 删除带有 XSLT 的 ns1 前缀

我在下面有以下 XML:

<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>

我需要创建一个肥皂信封以将我的 XML 文件发送到具有以下格式的 Web 服务:

<?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 代码:

<?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:
<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吗?

【问题讨论】:

  • 1.您显示的结果与我将您的 XSLT 应用于给定 XML 的结果非常不同。 -- 2. 为什么要在想要得到的结果中声明xmlns:ns1="http://servicos.embratec.com.br/esb?它不在任何地方使用。
  • 晚安,我同意。没有必要有 xmlns: ns1 = "servicos.embratec.com.br/esb。但是我需要删除前缀 ns1: 我的最终 XML 应该是:
  • servicos.embratec.com.br/esb" xmlns:soapenv="schemas.xmlsoap.org/soap/envelope">
    1174
  • 请不要在cmets中发布代码;改为编辑您的问题。

标签: xslt soap prefix envelope


【解决方案1】:

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

XSLT 1.0

<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 中复制节点也会复制该节点范围内的名称空间。因此,结果将包含一个冗余的命名空间声明:

<?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>

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

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多