英文:
Filter using xPathUtils.compile() on select
问题
我正在解析一个hdom对象,这是一个返回的资源列表。它们的格式如下所示:
示例XML如下:
<ResourceList>
<Resource>
<isValid>0</isValid>
<keyValue>ABC</keyValue>
</Resource>
<Resource>
<isValid>0</isValid>
<keyValue>PQR</keyValue>
</Resource>
<Resource>
<isValid>1</isValid>
<keyValue>XYZ</keyValue>
</Resource>
</ResourceList>
当我使用以下过滤器时,它会正确返回ABC
:
XpathUtils.compile("ResourceList/Resource/keyValue").select(SampleXML)
但我想根据isValid
标志为1
进行过滤,并返回键值XYZ
。请问如何在XpathUtils函数的.compile上应用过滤器?
抱歉,如果这个问题太基础,因为我对Java有些陌生。
尝试设置过滤器,但似乎得到null作为输出。
英文:
I am parsing a hdom object which is a list of resources returned. They are of the format
The Sample XML is as follows
<ResourceList>
<Resource>
<isValid>0</isValid>
<keyValue>ABC</keyValue>
</Resource>
<Resource>
<isValid>0</isValid>
<keyValue>PQR</keyValue>
</Resource>
<Resource>
<isValid>1</isValid>
<keyValue>XYZ</keyValue>
</Resource>
</ResourceList>
When I use the following filter, it correctly returns ABC
XpathUtils.compile("ResourceList/Resource/keyValue").select(SampleXML)
However I want to filter on isValid
flag 1
and return the key Value XYZ
.
How do I apply the filter on XpathUtils function .compile, please?
Sorry if its too basic as I am somewhat new to java.
Tried setting up filters but seem to get null as an output
答案1
得分: 0
使用XPath中的"predicate"(括在方括号中的布尔表达式)来筛选Resource
元素集,然后返回该筛选集中的keyValue
子元素。例如:
ResourceList/Resource[isValid='1']/keyValue
参考https://www.w3.org/TR/1999/REC-xpath-19991116/#predicates
英文:
Use an XPath "predicate" (a boolean expression enclosed in square brackets) to filter the set of Resource
elements, and then return the keyValue
child elements from that filtered set. e.g.
ResourceList/Resource[isValid='1']/keyValue
See https://www.w3.org/TR/1999/REC-xpath-19991116/#predicates
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论