XPath检查节点是否为空

3
使用XPath,如何检查节点或标签值是否为空?
给定样本XML:
问题(需要以下XPath表达式): 1.如何检查ExceptionMessage1是否为空。 2.如何检查ExceptionMessage2 @nil == true。 3.如果有ExceptionMessage3,它可以是以下任意一种情况: a.包含异常消息 b.没有消息,带有@nil="true"(结合了ExceptionMessage1和ExceptionMessage2的行为)
<Envelope>
   <Body>
      <SellResponse>
         <SellResult>
            <ExceptionMessage1>JourneyManager.SellJourney(segment): Segment already booked under different class of service</ExceptionMessage1>
            <ExceptionMessage2 nil="true"/>
            <NSProcessTime>0</NSProcessTime>
            <ProcessTime>0</ProcessTime>
            <Booking nil="true"/>
            <Success nil="true"/>
            <Warning nil="true"/>
            <Error nil="true"/>
            <OtherServiceInformations nil="true"/>
         </SellResult>
      </SellResponse>
   </Body>
</Envelope>

TIA


我不明白。你想要什么样的答案?你所说的“检查”是指什么,你希望XPath返回什么样的值? - har07
1
我需要一个表达式来检查ExceptionMessage1是否为空。同时,检查ExceptionMessage2的nil属性值是否为true。我期望两个表达式都能返回布尔结果。最后,如果有ExceptionMessage3,并且它有消息...它将与ExceptionMessage1一样显示,如果没有(意味着为空)...它将是带有属性nil="true"的空白。对于ExceptionMessage3,我想检查它是否不为空或nil=true。我只需要表达式。 - Borgy Manotoy
3个回答

6

"检查 ExceptionMessage1 是否为空"

如果ExceptionMessage1有子节点(非空),则以下内容应返回true,否则返回false

not(//ExceptionMessage1/node())

检查 ExceptionMessage2 是否为 true

我认为这很直接明了:

boolean(//ExceptionMessage2/@nil = 'true')

“使用ExceptionMessage3,我想检查它是否不为空或nil=true”

如果存在不为空的ExceptionMessage3或具有属性nil等于'true',则以下XPath返回true

boolean(//ExceptionMessage3[node() or @nil='true'])

如果上下文明确期望布尔值,则可以省略boolean()调用。在这种情况下,转换将自动进行。


@BorgyManotoy 看起来这个工具无法处理。我曾经在以下其中一个工具中尝试使用XPath:123 - har07
一切都正常工作了,问题出在我使用的xpath测试站点上。非常感谢har07。我喜欢这个测试工具(http://xpatheval.apphb.com)。也许是因为我使用的在线xpath测试工具的xpath版本问题。 - Borgy Manotoy
@BorgyManotoy 谢谢,实际上是我制作的(https://github.com/har07/XPathEval)! - har07

2

我想可能有人会遇到类似的问题,所以我把从Lech和har07那里收集到的解决方案发布出来,感谢你们。

我使用在线XPath测试器 (http://xpatheval.apphb.com)。

  1. how will I check if ExceptionMessage1 is blank or empty string.
    //ExceptionMessage1/text() = ''
    http://xpatheval.apphb.com/KHg6afoO2

    empty(//ExceptionMessage1/text())
    http://xpatheval.apphb.com/lZHxs8BO6

  2. how will I check if ExceptionMessage2 @nil == true?
    //ExceptionMessage2/@nil = 'true'
    http://xpatheval.apphb.com/8Tf7Kf8yx

    boolean(//ExceptionMessage2/@nil = 'true')
    http://xpatheval.apphb.com/_KcXIH9fU

    If the attribute nil's value is false, it will also return false

  3. how about if there will be ExceptionMessage3, it can either be:
    a. Contains exception message
    b. No message, with @nil="true" (Combined ExceptionMessage1 & ExceptionMessage2 behaviors)

    <Envelope>    
       <Body>    
           <SellResponse>    
               <SellResult>    
                   <ExceptionMessage1>JourneyManager.SellJourney(segment): Segment already booked under different class of service</ExceptionMessage1>    
                   <ExceptionMessage2 nil="true"/>    
                   <ExceptionMessage3 nil="false">JourneyManager.SellJourney(segment): No funds to proceed with the transaction</ExceptionMessage3>    
                   <NSProcessTime>0</NSProcessTime>    
                   <ProcessTime>0</ProcessTime>    
                   <Booking nil="true"/>    
                   <Success nil="true"/>    
                   <Warning nil="true"/>    
                   <Error nil="true"/>    
                   <OtherServiceInformations nil="true"/>    
               </SellResult>    
           </SellResponse>    
       </Body>    
    </Envelope>    
    

感谢Lech Rzedzicki和har07的帮助。


1
  1. //ExceptionMessage1/text() = '' - 这将检查元素内容是否为空字符串 - 可以在任何Xpath教程中搜索更多示例或者需要反转操作。
  2. //ExceptionMessage2/@nil = 'true'
  3. 只需使用or将上述两个Xpath组合起来即可。

我在http://www.webtoolkitonline.com/xml-xpath-tester.html上尝试了一下,但它不接受... #1 - 表达式( = '')返回TypeError。当我将nil属性的值更改为"false"时,#2会得到TypeError。 - Borgy Manotoy
谢谢兄弟,两个解决方案都对我有用。问题出在我用来测试的在线xpath测试工具上。 - Borgy Manotoy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接