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

Michele Vivoda commented on JXPATH-182:
---------------------------------------

I didn't know that to access a {{foo}} property of an object or of a map one 
can use {{foo}} but also {{.\[@name='foo'\]}}, is documented  here 
https://commons.apache.org/proper/commons-jxpath/users-guide.html#Map_Element_Access

What is really strange to me is that, in the context of an object or of a map, 
{{.}} means the map/obj while {{.\[@name='xx'\]}} means the value of the {{xx}} 
property, this is not possible with standard xpath, is not possible to change 
context using a test predicate.

Following this logic if {{.\[@name='foo'\]}} is itself a map/object then 
{{.\[@name='foo'\]\[@name='bar'\]}}  behaves as {{foo/bar}} and not as an 
impossible to match xpath as one would expect, and 
{{.\[@name='foo'\]/.[@name='bar'\]}} is just an other way of writing it.

I discovered this because in the process of "fixing" the way the path is built, 
adding a {{./}} in between each segment, I found many tests failing. For 
example {{testMapMap}} selects {{map/map/string}} and expects from {{asPath()}} 
the xpath  {{/map\[@name='map'\]\[@name='string'\]}} (the first {{map}} is a 
property of a bean)

Strange also that the xpath of the failing test of this issue works up to 
{{/.\[@name='data']\[1]\[@name='data']}} then 
{{/.\[@name='data']\[1]\[@name='data']\[1]}} fails while 
{{/.\[@name='data']\[1]\[@name='data']/.\[1]}} works

Fixing the path to be {{/.\[\@name='data']\[1]/.\[@name='data']\[1]}} has the 
problem that many tests (~26) expect different values. I added a "roundtrip" 
step also to the 55 tests that check the returned xpath and they all work, is 
only the example of this issue that is not able to select the same value with 
the xpath from #asPath()

After these findings I am now wondering why the return value of asPath() is 
built using {{/.\[@name='foo'\]/.\[@name='bar'\]}} and not as for beans, using 
{{/foo/bar}} that is also a standard, valid XPath while the other is a custom 
thing of JXPath.  I don't think this is specified anywhere, changing it would 
require to change a lot of tests but could be a good thing.

The implementation for asPath() for Map is done in {{DynamicPropertyPointer}}, 
for beans in {{NodePointer}}.

> asPath() returns XPath that can't be found in the context
> ---------------------------------------------------------
>
>                 Key: JXPATH-182
>                 URL: https://issues.apache.org/jira/browse/JXPATH-182
>             Project: Commons JXPath
>          Issue Type: Bug
>            Reporter: Evgeniy Sharapov
>         Attachments: JXPath182Test.java, JXpath182Test.java
>
>
> When I get the pointer and then call its {{asPath}} method and use that path 
> to find a pointer in the same context {{JXPathContext}} can't find it.
> In the attached test 
> I get the pointer by using {{"data[1]/data[1]"}}
> {{asPath()}} then returns {{"/.[@name='data'][1][@name='data'][1]"}}
> But in fact it should be {{/.[@name='data'][1]/.[@name='data'][1]}}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to