My xml structure is something like this :
<page height="777" width="777">
<block r="777" l="778" blockType="Text">
<region/>
<separator/>
</block>
<block r="777" l="790" blockType="Text">
<region/>
<separator/>
</block>
<block r="777" l="688" blockType="Text">
<region/>
<text/>
</block>
</page>
i want the select the block node which has the min "l" value i.e "688"
I know there is a min() function available in XPath 2.0, but i am using Xpath1.0.
I tried this
//XPathExpression pattern2TextExpr =
xPath.compile("//page/block[not(preceding-sibling::block/@l < = @l) and not(following-sibling::block/@l >@l)]/@l");
How can I use XPath to find the minimum value of an attribute in a set of elements?
But this gives me multiple nodelist. if it's min it should be only one node. Any other possible and efficient solution to this ?? Please suggest. Thanks
UPDATE 2:
According to the updated XML sample, assuming that you want to find minimum value of
l
attribute ofline
element, try this way :output :
UPDATE :
Actually, by having combination of
preceding-sibling::block/@l <= @l
andfollowing-sibling::block/@l < @l
, there is no chance you get duplicate anyway. The xpath returns only the first minimum value in case of duplicate values exists, because of<=
. The real problem I found in your xpath is, that you use>
in thefollowing-sibling
evaluation instead of using<
.If the only catch is that your xpath may return multiple values, then you can simply wrap the entire xpath with
()[1]
to limit the result to single value, for example (formatted for readability) :output in xpath tester :