2015-09-30 58 views
0

我需要过滤SOAP Web服务的响应。由于SOAP基于XML,因此我正在考虑使用libxml2,但我无法理解如何编写XPath表达式来实现所需的结果。使用基于子元素内容的XPath过滤XML

在消息结尾处,您会看到一个响应示例,其中发送了两个NotficationMessages,其中一个主题为tns1:RuleEngine/LineDetector/Crossed,另一个主题为tns1:RuleEngine/CellMotionDetector/Motion

我想写以下XPath表达式:

  • 比赛,其主题是tns1:RuleEngine/LineDetector/Crossed
  • 比赛,其主题是tns1:RuleEngine//.
  • 比赛,其主题是一切,但tns1:RuleEngine//.
  • 任何NotficationMessage任何NotficationMessage任何NotficationMessage

我发现的所有例子都与属性匹配,而不是ch的内容ild元素。

所以我问。

  1. 这种类型的匹配可以在libxml2或XPath中使用吗?
  2. 你能给我一个关于编写XPath表达式的提示吗?
<?xml version="1.0" encoding="UTF-8"?> 
    <SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:wsa="http://www.w3.org/2005/08/addressing" 
    xmlns:wstop="http://docs.oasis-open.org/wsn/t-1" 
    xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" 
    xmlns:tet="http://www.onvif.org/ver10/events/wsdl" 
    xmlns:tns1="http://www.onvif.org/ver10/topics" 
    xmlns:tt="http://www.onvif.org/ver10/schema"> 
    <SOAP-ENV:Header> 
     <wsa:Action> http://www.onvif.org/ver10/events/wsdl/PullPointSubscription/PullMessagesResponse 
     </wsa:Action> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
     <tet:PullMessagesResponse> 
     <tet:CurrentTime> 
      2008-10-10T12:24:58 
     </tet:CurrentTime> 
     <tet:TerminationTime> 
      2008-10-10T12:25:58 
     </tet:TerminationTime> 
     <wsnt:NotificationMessage> 
      <wsnt:Topic Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet"> 
      tns1:RuleEngine/LineDetector/Crossed 
      </wsnt:Topic> 
      <wsnt:Message> 
      <tt:Message UtcTime="2008-10-10T12:24:57.321Z"> 
       <tt:Source> 
       <tt:SimpleItem Name="VideoSourceConfigurationToken" 
           Value="1"/> 
       <tt:SimpleItem Name="VideoAnalyticsConfigurationToken" 
           Value="2"/> 
       <tt:SimpleItem Value="MyImportantFence1" Name="Rule"/> 
       </tt:Source> 
       <tt:Data> 
       <tt:SimpleItem Name="ObjectId" Value="15" /> 
       </tt:Data> 
      </tt:Message> 
      </wsnt:Message> 
     </wsnt:NotificationMessage> 
     <wsnt:NotficationMessage> 
      <wsnt:Topic Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet"> 
      tns1:RuleEngine/CellMotionDetector/Motion 
      </wsnt:Topic> 
      <wsnt:Message> 
      <tt:Message UtcTime= "2010-10-20T12:24:57.628"> 
       <tt:Source> 
       <tt:SimpleItem Name="VideoSourceConfigurationToken" Value="1"/> 
       <tt:SimpleItem Name="VideoAnalyticsConfigurationToken" Value="1"/> 
       <tt:SimpleItem Name="Rule" Value="MotionInDefinedCells"/> 
       </tt:Source> 
       <tt:Data> 
       <tt:SimpleItem Name="IsMotion" Value="true"/> 
       </tt:Data> 
      </tt:Message> 
      </wsnt:Message> 
     </wsnt:NotficationMessage> 
     </tet:PullMessagesResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

回答

0

所有你需要的是使用location pathspredicates基本XPath表达式:

  1. 比赛,其主题是tns1:RuleEngine/LineDetector/Crossed

    //wsnt:NotificationMessage[wsnt:Topic = 'tns1:RuleEngine/LineDetector/Crossed'] 
    
  2. 比赛,其主题是tns1:RuleEngine//.任何NotficationMessage任何NotficationMessage

    //wsnt:NotificationMessage[wsnt:Topic = 'tns1:RuleEngine//.'] 
    
  3. 匹配任何NotficationMessage,其主题是一切,但tns1:RuleEngine//.

    //wsnt:NotificationMessage[wsnt:Topic != 'tns1:RuleEngine//.']