首页 文章

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

提问于
浏览
0

我是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[at this point I want to put a validation which will return true or false based on some check of isNull or notNull].

1 回答

  • 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()]))
    

相关问题