【问题标题】:Query on XPATH Statement查询 XPATH 语句
【发布时间】:2010-11-12 13:10:23
【问题描述】:

我必须编写一个通用模板来从肥皂响应中查找操作名称和命名空间。我的意思是通用的,适用于任何操作。所以我不知道操作名称和命名空间名称,但想在发送响应时获取它们并修改回来。

这是响应的结构:

类型1:操作的命名空间定义在<soapenv:Envelope>:

<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:inf="http://bad.com/abc/infrastructure" 
  xmlns:ret="http://bad.com/FirstOperationToExecute" 
  xmlns:com="http://bad.com/commercial">
  <soapenv:Header/>
  <soapenv:Body>
    <ret:retrieveSummaryRequest>
      <!-- ... result ... -->
    </ret:retrieveSummaryRequest>
  </soapenv:Body>
</soapenv:Envelope>

类型2:在元素&lt;ret:retrieveSummaryRequest&gt;中定义的命名空间:

<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:inf="http://bad.com/abc/infrastructure" 
  xmlns:ret="http://bad.com/FirstOperationToExecute" 
  xmlns:com="http://bad.com/commercial">
  <soapenv:Header/>
  <soapenv:Body>
    <ret:retrieveSummaryRequest xmlns:ret="http://bad.com/FirstOperationToExecute" > 
      <!-- ... result ... -->
    </ret:retrieveSummaryRequest>
  </soapenv:Body>
</soapenv:Envelope>

类型 3:&lt;retrieveSummaryRequest&gt; 中的默认命名空间:

<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:inf="http://bad.com/abc/infrastructure" 
  xmlns:ret="http://bad.com/FirstOperationToExecute" 
  xmlns:com="http://bad.com/commercial">
  <soapenv:Header/>
  <soapenv:Body>
    <retrieveSummaryRequest xmlns="http://bad.com/FirstOperationToExecute" > 
      <!-- ... result ... -->
    </retrieveSummaryRequest>
  </soapenv:Body>
</soapenv:Envelope>

谁能帮我看看是否有一个简单的 XPath 语句来获取操作名称和命名空间。

【问题讨论】:

    标签: xml xpath namespaces


    【解决方案1】:

    以上所有 XML 示例都代表等效的信息集。考虑到所有因素,它们相等。这些差异仅影响序列化(文本)表单,因此它们不应该打扰您。

    如果我没听错的话,你想检索这两个值:

    • "http://bad.com/FirstOperationToExecute"
    • "FirstOperationToExecute"

    要在 XSLT 中获取这些值,请使用:

    <xsl:template match="ret:retrieveSummaryRequest">
      <!-- the full URI -->
      <xsl:value-of select="namespace-uri()" />
    
      <!-- the operation name only -->
      <xsl:value-of select="
        substring-after(
          substring-after(namespace-uri(), '//'), 
          '/'
        )
      " />
    </xsl:template>
    

    如果您不在 XSLT 中,那么检索操作名称就是选择正确的节点并向 DOM 询问名称空间 URI。伪代码:

    dom.registerNSPrefix("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
    ret    = dom.selectSingleNode("/soapenv:Envelope/soapenv:Body/*[1]");
    nsUri  = ret.namespaceURI;
    opName = nsUri.replace(".*/", ""); // whatever
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 2012-02-03
      • 1970-01-01
      相关资源
      最近更新 更多