下午好,
我需要使用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>我需要创建一个soap信封,以便将我的XML文件发送到以下格式的need服务:
<?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吗?
发布于 2020-07-04 05:30:23
您可以使用以下命令获得在语义上与您想要的结果相同的结果:
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>https://stackoverflow.com/questions/62722305
复制相似问题