Executing xpath query with number in path

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Executing xpath query with number in path

Roy Teeuwen
Hey all,

In the framework that we use (AEM) there is a folder structure as following:

/content/launches/2019/08/29/my-launch/content/site-name

I would like to do a query to search for subresources in this site, but it seems that this gives issues because an xpath query cannot take numbers as path names:

/jcr:root/content/launches/2019(*)/08/29/my-launch/content/site-name//*[@sling:resourceType='some/components/path']; expected: jcr:root, /, *, @, (, .

Is there any way around this to still make it work with xpath queries? Can I escape it in any way? Or can I say "start the query below /content/launches/2019/08/29/my-launch/content/site-name so that it doesnt see the numbers in the query

Thanks!
Roy

Reply | Threaded
Open this post in threaded view
|

Re: Executing xpath query with number in path

Björn Weide
Hi Roy,

you need to escape it - in AEM you can use
https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/day/text/ISO9075.html
(or
use SQL2 for the query).

Best regards,
Bjoern

On 29 August 2019 at 17:54:16, Roy Teeuwen ([hidden email]) wrote:

Hey all,

In the framework that we use (AEM) there is a folder structure as
following:

/content/launches/2019/08/29/my-launch/content/site-name

I would like to do a query to search for subresources in this site, but it
seems that this gives issues because an xpath query cannot take numbers as
path names:

/jcr:root/content/launches/2019(*)/08/29/my-launch/content/site-name//*[@sling:resourceType='some/components/path'];
expected: jcr:root, /, *, @, (, .

Is there any way around this to still make it work with xpath queries? Can
I escape it in any way? Or can I say "start the query below
/content/launches/2019/08/29/my-launch/content/site-name so that it doesnt
see the numbers in the query

Thanks!
Roy
Reply | Threaded
Open this post in threaded view
|

Re: Executing xpath query with number in path

Eric Norman
In reply to this post by Roy Teeuwen
If I recall correctly, if you want to use paths in XPath queries then you
need to escape the path segments according to ISO9075 rules.   For your
referenced, there is a brief section about that on the wiki at [1].

Also, there is a utility class in jackrabbit that can do the ISO9075 path
encoding for you at [2] that you may utilize.

1.
http://jackrabbit.apache.org/archive/wiki/JCR/EncodingAndEscaping_115513396.html
2.
http://jackrabbit.apache.org/api/trunk/org/apache/jackrabbit/util/ISO9075.html

I hope that helps.

On Thu, Aug 29, 2019 at 8:54 AM Roy Teeuwen <[hidden email]> wrote:

> Hey all,
>
> In the framework that we use (AEM) there is a folder structure as
> following:
>
> /content/launches/2019/08/29/my-launch/content/site-name
>
> I would like to do a query to search for subresources in this site, but it
> seems that this gives issues because an xpath query cannot take numbers as
> path names:
>
> /jcr:root/content/launches/2019(*)/08/29/my-launch/content/site-name//*[@sling:resourceType='some/components/path'];
> expected: jcr:root, /, *, @, (, .
>
> Is there any way around this to still make it work with xpath queries? Can
> I escape it in any way? Or can I say "start the query below
> /content/launches/2019/08/29/my-launch/content/site-name so that it doesnt
> see the numbers in the query
>
> Thanks!
> Roy
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Executing xpath query with number in path

Sergiu Dumitriu
In reply to this post by Roy Teeuwen
It seems that there's a mismatch between what is a valid XML name (which
is what XPath is supposed to be working with) and what is a valid JCR
node name.

XPath accepts an XML name in a path [1], which by definition cannot
start with a number [2].

JCR says that local node names can start with anything except a few
special characters like /:[]|*, which have a special meaning in XPath
expressions [3].

It's obvious from these definitions that there are names which are valid
in XML but not in JCR (those starting with ":" for example), and names
which are valid in JCR but not in XML (those starting with numbers or
space, for example). Although XPath is an excellent choice for a query
language for a tree structure, the fact that it targets a different
technology will sometimes highlight this mismatch. An alternative would
be to use something like /content/launches/*[local-name() = '2019'] but
this doesn't seem to be supported.

So, maybe use JCR-SQL2 instead?

Oh, another option, if you can make those node referenceable, you could
use their UUID instead of their name:

//*[@jcr:uuid='78d6b28d-8c91-4ba8-b2de-bb0434533e88']/my-launch/...

[1] https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-NameTest
[2] https://www.w3.org/TR/REC-xml/#d0e804
[3]
https://docs.adobe.com/docs/en/spec/jcr/2.0/3_Repository_Model.html#3.2.2%20Local%20Names

On 8/29/19 11:54 AM, Roy Teeuwen wrote:

> Hey all,
>
> In the framework that we use (AEM) there is a folder structure as following:
>
> /content/launches/2019/08/29/my-launch/content/site-name
>
> I would like to do a query to search for subresources in this site, but it seems that this gives issues because an xpath query cannot take numbers as path names:
>
> /jcr:root/content/launches/2019(*)/08/29/my-launch/content/site-name//*[@sling:resourceType='some/components/path']; expected: jcr:root, /, *, @, (, .
>
> Is there any way around this to still make it work with xpath queries? Can I escape it in any way? Or can I say "start the query below /content/launches/2019/08/29/my-launch/content/site-name so that it doesnt see the numbers in the query
>
> Thanks!
> Roy
>


--
Sergiu Dumitriu
http://purl.org/net/sergiu
Reply | Threaded
Open this post in threaded view
|

Re: Executing xpath query with number in path

Julian Sedding-3
In reply to this post by Eric Norman
Hi Roy

Like others said before, ISO9075 is what you need. You only need to
encode the first digit. The encoding is simple enough to remember (if
used regularly ;) ):

0: _x0030_
1: _x0031_
2: _x0032_
...

Your query would thus become:
/jcr:root/content/launches/_x0032_019/_x0030_8/_x0032_9/my-launch/content/site-name//*[@sling:resourceType='some/components/path']

As a shortcut in situations where the depth is known I sometimes use:
/jcr:root/content/launches/*/*/*/my-launch/content/site-name//*[@sling:resourceType='some/components/path']

Another alternative would be:
/jcr:root/content/launches/*[fn:name() = '2019']/*[fn:name() =
'08']/*[fn:name() =
'29']/my-launch/content/site-name//*[@sling:resourceType='some/components/path']

I believe that the ISO9075 encoded version performs best, but I didn't
verify that claim. Most likely performance is not your main concern in
any case, as all of the above should be reasonable fast  ;)

Regards
Julian

On Thu, Aug 29, 2019 at 6:52 PM Eric Norman <[hidden email]> wrote:

>
> If I recall correctly, if you want to use paths in XPath queries then you
> need to escape the path segments according to ISO9075 rules.   For your
> referenced, there is a brief section about that on the wiki at [1].
>
> Also, there is a utility class in jackrabbit that can do the ISO9075 path
> encoding for you at [2] that you may utilize.
>
> 1.
> http://jackrabbit.apache.org/archive/wiki/JCR/EncodingAndEscaping_115513396.html
> 2.
> http://jackrabbit.apache.org/api/trunk/org/apache/jackrabbit/util/ISO9075.html
>
> I hope that helps.
>
> On Thu, Aug 29, 2019 at 8:54 AM Roy Teeuwen <[hidden email]> wrote:
>
> > Hey all,
> >
> > In the framework that we use (AEM) there is a folder structure as
> > following:
> >
> > /content/launches/2019/08/29/my-launch/content/site-name
> >
> > I would like to do a query to search for subresources in this site, but it
> > seems that this gives issues because an xpath query cannot take numbers as
> > path names:
> >
> > /jcr:root/content/launches/2019(*)/08/29/my-launch/content/site-name//*[@sling:resourceType='some/components/path'];
> > expected: jcr:root, /, *, @, (, .
> >
> > Is there any way around this to still make it work with xpath queries? Can
> > I escape it in any way? Or can I say "start the query below
> > /content/launches/2019/08/29/my-launch/content/site-name so that it doesnt
> > see the numbers in the query
> >
> > Thanks!
> > Roy
> >
> >