使用xPathUtils.compile()来筛选选择。

huangapple go评论53阅读模式
英文:

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

&lt;ResourceList&gt;     
    &lt;Resource&gt;
        &lt;isValid&gt;0&lt;/isValid&gt;
        &lt;keyValue&gt;ABC&lt;/keyValue&gt;
    &lt;/Resource&gt;     
    &lt;Resource&gt; 
        &lt;isValid&gt;0&lt;/isValid&gt;    
        &lt;keyValue&gt;PQR&lt;/keyValue&gt;  
    &lt;/Resource&gt;     
    &lt;Resource&gt;
        &lt;isValid&gt;1&lt;/isValid&gt;
        &lt;keyValue&gt;XYZ&lt;/keyValue&gt;
     &lt;/Resource&gt;
&lt;/ResourceList&gt; 

When I use the following filter, it correctly returns ABC XpathUtils.compile(&quot;ResourceList/Resource/keyValue&quot;).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=&#39;1&#39;]/keyValue

See https://www.w3.org/TR/1999/REC-xpath-19991116/#predicates

huangapple
  • 本文由 发表于 2023年2月16日 04:17:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定