检查元素节点是否包含使用java和Xpath的文本?

时间:2016-06-17 09:21:39

标签: java xpath

我是Xpath的新手。我遇到一个问题,我必须得到Xpath的布尔响应,如果一个元素不包含任何文本,那么它应该返回false,否则为true。我看过很多例子,而且我没有太多时间学习Xpath表达式。下面是Xml文件。

<?xml version="1.0" encoding="UTF-8" ?>
<order id="1234" date="05/06/2013">
  <customer first_name="James" last_name="Rorrison">
    <email>j.rorri@me.com</email>
    <phoneNumber>+44 1234 1234</phoneNumber>
  </customer>
  <content>
    <order_line item="H2G2" quantity="1">
      <unit_price>23.5</unit_price>
    </order_line>
    <order_line item="Harry Potter" quantity="2">
      <unit_price></unit_price>//**I want false here**
    </order_line>
  </content>
  <credit_card number="1357" expiry_date="10/13" control_number="234" type="Visa" />
</order>

你能否指出为这个问题创建xpath表达式的正确方向。

我想要的是一个表达式(虚拟表达式),如下所示。

/ order / content / order_line / unit_price [此时我想根据对isNull或notNull的一些检查进行验证,返回true或false]。

1 个答案:

答案 0 :(得分:1)

以下xpath将执行此操作:

not(boolean(//*[not(text() or *)]))

但是这个xpath还包含credit_card节点,因为它不包含任何文本(属性不是text())。

如果您还想要使用属性排除节点,请使用此..

not(boolean(//*[not(text() or * or @*)]))

编辑后,你可以这样做..

/order/content/order_line/unit_price[not(text()]

它将返回一个没有文本的节点列表,从那里你可以测试你的测试节点数。

或返回true / false ..

not(boolean(/order/content/order_line/unit_price[not(text()]))
相关问题