[ 
https://issues.apache.org/jira/browse/JXPATH-163?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13886230#comment-13886230
 ] 

Michele Vivoda edited comment on JXPATH-163 at 1/30/14 4:02 AM:
----------------------------------------------------------------

Hi,

>From XPath specification:

http://www.w3.org/TR/xpath/#NT-PredicateExpr

[8]     Predicate          ::=          '[' PredicateExpr ']'   
[9]     PredicateExpr      ::=          Expr

A predicate filters a node-set with respect to an axis to produce a new 
node-set. For each node in the node-set to be filtered, the PredicateExpr is 
evaluated with that node as the context node, with the number of nodes in the 
node-set as the context size, and with the proximity position of the node in 
the node-set with respect to the axis as the context position; if PredicateExpr 
evaluates to true for that node, the node is included in the new node-set; 
otherwise, it is not included.

A PredicateExpr is evaluated by evaluating the Expr and converting the result 
to a boolean. If the result is a number, the result will be converted to true 
if the number is equal to the context position and will be converted to false 
otherwise; if the result is not a number, then the result will be converted as 
if by a call to the boolean function. Thus a location path para[3] is 
equivalent to para[position()=3].

--------------

I read that if the Expr is a string, it is converted to true if the string is 
not empty, meaning 'any node' and so the first node is selected, if it were a 
"node set" selection instead of a "single node" then all nodes would have been 
selected. JXPath seems to comply with the spec.

You may probably consider to use  number() to force conversion to a number that 
then is checked against the position.

/se:someArray[number($var)]

or also in this way:

/se:someArray[position() = $var]


 



was (Author: vivodamich...@hotmail.com):
Hi,

>From XPath specification:

http://www.w3.org/TR/xpath/#NT-PredicateExpr

[8]     Predicate          ::=          '[' PredicateExpr ']'   
[9]     PredicateExpr      ::=          Expr

A predicate filters a node-set with respect to an axis to produce a new 
node-set. For each node in the node-set to be filtered, the PredicateExpr is 
evaluated with that node as the context node, with the number of nodes in the 
node-set as the context size, and with the proximity position of the node in 
the node-set with respect to the axis as the context position; if PredicateExpr 
evaluates to true for that node, the node is included in the new node-set; 
otherwise, it is not included.

A PredicateExpr is evaluated by evaluating the Expr and converting the result 
to a boolean. If the result is a number, the result will be converted to true 
if the number is equal to the context position and will be converted to false 
otherwise; if the result is not a number, then the result will be converted as 
if by a call to the boolean function. Thus a location path para[3] is 
equivalent to para[position()=3].

--------------

I read that if the Expr is a string, it is converted to true if the string is 
not empty, meaning 'any node' and so the first node is selected, if it were a 
"node set" selection instead of a "single node" then all nodes would have been 
selected. JXPath seems to comply with the spec.

You may probably consider to use  number() to force conversion to a number that 
then is checked against the position.

/se:someArray[number($var)]

 


> String variables cannot be used as indices
> ------------------------------------------
>
>                 Key: JXPATH-163
>                 URL: https://issues.apache.org/jira/browse/JXPATH-163
>             Project: Commons JXPath
>          Issue Type: Bug
>    Affects Versions: 1.3
>            Reporter: Alexander Koledzhikov
>            Priority: Minor
>         Attachments: commons-jxpath.patch
>
>
> If I have a string variable, I cannot use it as an index in an xpath query.
> For example, if var = "5", this query will return the 1st element, not the 
> fifth:
> /se:someArray[$var]



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to