我有以下 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 表达式,如果具有这些值之一的 class
的 product/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/