查询XPATH语句

时间:2009-07-21 05:33:28

标签: xml xpath namespaces

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

以下是回复的结构:

类型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:元素<ret:retrieveSummaryRequest>中定义的命名空间:

<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:<retrieveSummaryRequest>中的默认命名空间:

<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语句来获取操作名称和命名空间。

1 个答案:

答案 0 :(得分:0)

以上所有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
相关问题