“ns1”和“tns”标签之间的区别

MPA*_*A95 1 php soap

我正在尝试使用 PHP 的本机 SOAP 库实现的另一个端点来复制传统的 NuSOAP 端点。我通过在每个端点中返回相同的虚拟数组并比较每个端点返回的 xml 进行测试。

以下是旧服务器给出的示例响应

<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" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/webservices-soap/servicios">
   <SOAP-ENV:Body>
      <ns1:LeerAtributosResponse xmlns:ns1="http://localhost/webservices-soap/servicios">
         <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:ParamValue[2]">.
            <item xsi:type="tns:ParamValue">
               <parametro xsi:type="xsd:string">hola</parametro>
               <valor xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[3]">
                  <item xsi:type="xsd:string">5</item>
                  <item xsi:type="xsd:string">6</item>
                  <item xsi:type="xsd:string">7</item>
               </valor>
            </item>
            <item xsi:type="tns:ParamValue">
               <parametro xsi:type="xsd:string">chau</parametro>
               <valor xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[0]"/>
            </item>
         </return>
      </ns1:LeerAtributosResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

这是新实现给出的响应:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost:8000/soap/servicios" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>
      <ns1:LeerAtributosResponse>
         <return SOAP-ENC:arrayType="ns1:ParamValue[2]" xsi:type="ns1:ArrayOfParamValue">
            <item xsi:type="ns1:ParamValue">
               <parametro xsi:type="xsd:string">hola</parametro>
               <valor SOAP-ENC:arrayType="xsd:string[3]" xsi:type="ns1:ArrayOfString">
                  <item xsi:type="xsd:string">5</item>
                  <item xsi:type="xsd:string">6</item>
                  <item xsi:type="xsd:string">7</item>
               </valor>
            </item>
            <item xsi:type="ns1:ParamValue">
               <parametro xsi:type="xsd:string">chau</parametro>
               <valor xsi:type="ns1:ArrayOfString"/>
            </item>
         </return>
      </ns1:LeerAtributosResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

我发现回复之间存在以下差异:

  • 在节点 'ns1:LeerAtributosResponse' 中有一个额外的标签
  • 在许多字段中,新响应使用“ns1”前缀,而不是像旧响应那样使用“tns”

这两个标签(“ns1”和“tns”)有什么区别?xml 中的这种差异是否会导致响应不同?

IMS*_*SoP 8

您在这里看到的是“XML 命名空间”。简而言之:

  • XML 命名空间由 URI 唯一标识;URI 不必指向任何地方,它只是“拥有”名称的一种方式。例如,SOAP 信封信息使用命名空间http://schemas.xmlsoap.org/soap/envelope/
  • 在 XML 文档甚至 XML 文档的一部分中,命名空间被赋予“本地前缀”,这并不意味着该文档或部分之外的任何内容,只是为了避免多次写入整个 URI。这些是用诸如 、 之类的属性来声明的xmlns:someprefix="http://example.com",并在元素和属性名称中使用,比如<someprefix:foo someprefix:bar="hello" />
  • 每个部分还有一个“默认命名空间”,声明如下xmlns="http://example.com",它定义了没有前缀的元素所在的命名空间。
  • SOAP 服务通常会将其自定义标记放在该服务的特定命名空间中。在这种情况下,软件尝试根据您运行代码的位置构建一个命名空间,并提出http://localhost/webservices-soap/servicioshttp://localhost:8000/soap/servicios。命名空间实际上应该是硬编码的,以便无论在哪里部署服务它都是相同的,但这对于测试来说并不重要。
  • 如果您手动编写 XML,通常会为每个命名空间分配易于记忆的前缀,例如SOAP-ENVorsoapenvelopefor http://schemas.xmlsoap.org/soap/envelope/,但这些根本不会改变含义。在本例中,前缀tnsns1正是不同 SOAP 库选择使用的前缀。

举个简单的例子,这些 XML 片段具有完全相同的含义:

<?xml version="1.0"?>
<abc:example xmlns:abc="http://example.com">
     <abc:foo>42</abc:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)

和:

<?xml version="1.0"?>
<xyz:example xmlns:xyz="http://example.com">
     <xyz:foo>42</xyz:foo>
</xyz:example>
Run Code Online (Sandbox Code Playgroud)

乃至:

<?xml version="1.0"?>
<abc:example xmlns:abc="http://example.com">
     <xyz:foo xmlns:xyz="http://example.com">42</xyz:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)

但这意味着不同的东西(记住前缀并不重要,只有 URI):

<?xml version="1.0"?>
<abc:example xmlns:abc="http://something.different.example.com">
     <abc:foo>42</abc:foo>
</abc:example>
Run Code Online (Sandbox Code Playgroud)