java - 无效的 XPath 表达式 : expecting token

标签 java xpath

我有以下 XML 负载:

<?xml version="1.0" encoding="UTF-8" ?>
<product>
  <discount class="standard">
    <id>123</id>
    <beginDate>20181205</beginDate>
    <endDate>20181225</endDate>
  </discount>
  <account>12345</account>
</product>

discount 元素上的 class 属性可以具有以下值:

  • 标准
  • 特殊
  • 自定义
  • 促销

我正在尝试编写一个 XPath 表达式,如果具有这些值之一的 classproduct/discount elemtn,该表达式将匹配。我最好的尝试:

/product/discount[@class]/[@class = 'standard' or @class = 'special' or @class = 'customer' or @class = 'sale']

产生以下错误:

InvalidXPathExpression: Invalid xpath: /product/discount[@class]/[@class = 'standard' or @class = 'special' or @class = 'customer' or @class = 'sale']. Reason: javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.

我的 XPath 有什么问题吗?

最佳答案

如错误消息所示,/ 后面需要跟一个定位步骤。因此,/[@class = 'standard' ...] 不是有效的 xPath 表达式。

相反,请尝试:

/product/discount[@class = 'standard' or @class = 'special' or @class = 'customer' or @class = 'sale']

关于java - 无效的 XPath 表达式 : expecting token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533042/

相关文章:

java - 是否可以在我的 S3 lambda 函数中创建文件?

Java将原始字节编码为图像简单图像格式/文件

ruby - 如何搜索单个节点,而不是所有节点

css - Selenium Python 我正在尝试使用 XPATH 或 CSS 选择器从嵌套的 span 标记中选择一个下拉列表

java - Java 9 中的字符串连接是如何实现的?

java - servlet+ajax+jsp

java - 我应该在哪里进行改造操作?

xml - 提取XML属性和节点值R

java - 在 Selenium Webdriver 中按 xPath 元素搜索

xpath - Xpath表达式解释