2 Replies Latest reply on Mar 10, 2010 11:37 PM by Randall Hauch

    Testing XPath in ModeShape

    Bertrand Goetzmann Newbie

      Hello,

       

      I'm doing some experiments with ModeShape in a project that uses GMaven (Groovy+Maven).

       

      The Java/Groovy application uses an in memory repository; first I create a node like this:

       

      def session = createSession(repository, workspaceName)

      def node = session.rootNode.addNode('odl:odelia-technologies')

      node.setProperty('odl:url', 'http://www.odelia-technologies.com')

       

      session.save()

       

       

      The node is well created and I can display the url value:

       

      def n = session.rootNode.getNode('odl:odelia-technologies')

      println n.getProperty('odl:url') // displays odl:url=http://www.odelia-technologies.com

      Now I'm trying to get my node using a XPath query :
      def queryManager = session.workspace.queryManager
      def query = queryManager.createQuery("//odl:odelia-technologies", Query.XPATH)
      def result = query.execute()
      println "${result.nodes.size()} noeud(s) trouvé(s)"
      result.nodes.each {
        println it.name
      }
      It's OK and I get "1 noeud(s) trouvé(s)" followed by "odl:odelia-technologies"
      Now I try to be more restrictive and change the XPath to "//odl:odelia-technologies[@odl:url='http://www.odelia-technologies.com']", in order to
      get all nodes named "odl:odelia-technologies" and that have a "odl:url" with a value equals to "http://www.odelia-technologies.com".
      In that case, I get the display "0 noeud(s) trouvé(s)"!
      What's wrong in my XPath?
      Thank you for any help.
      Bertrand.
        • 1. Re: Testing XPath in ModeShape
          Randall Hauch Master

          First of all, thanks for giving ModeShape a try.

           

          I finally was able to duplicate your problem in a test case, and for me queries such as the following:

           

          //odl:odelia-technologies[@odl:url='http://www.odelia-technologies.com']

           

          do not find nodes that are immediately under the root, but it does find nodes that match that criteria in any location other than right under the root. For example, in your case, the "/odl:odelia-technologies" node should match the criteria, but for some reason it isn't included in the results. I think if you tried and there were nodes matching the criteria other than right under the root, the queries will work.

           

          Also, the following query:

           

          //odl:odelia-technologies

           

          does work because it is translated into different criteria (a simple name match), and thus this works regardless of where the node is.

           

          This behavior is definitely a bug because both queries are valid and should work, so I'll log a defect and we'll get that fixed in 1.1. Luckily, in this case there are several simple workarounds:

           

          1) In this example, the node you're searching for is right under the root, so you could constrain the query a bit more (resulting in different constraints that are processed correctly for all nodes):

           

          /jcr:root/odl:odelia-technologies[@odl:url='http://www.odelia-technologies.com']

           

          2) In the case where the location of the nodes is more variable (or unknown) but you know the name, you can use the 'element(name)' function to constrain the name of the node:

           

          //element(odl:odelia-technologies)[@odl:url='http://www.odelia-technologies.com']

           

          Hopefully these will work for you, and we'll get this fixed in 1.1, which we hope to release in a few weeks!

          • 2. Re: Testing XPath in ModeShape
            Randall Hauch Master
            This issue has been logged as MODE-686, and I just committed the fix into trunk.