tree: ac3b79bed698679c36fcbbbd2076e15dcaaa1ef0 [path history] [tgz]
  1. src/
  2. pom.xml


SlingQuery is a Sling resource tree traversal tool inspired by the jQuery.


Recommended way to find resources in the Sling repository is using tree-traversal methods, like listChildren() and getParent() rather than JCR queries. The latter are great for listing resources with given properties, but we can't leverage the repository tree structure with such queries. On the other hand, using tree-traversal method is quite verbose. Consider following code that takes an resource and returns its first ancestor, being cq:Page, with given jcr:content/cq:template attribute:

Resource resource = ...;
while ((resource = resource.getParent()) != null) {
    if (!resource.isResourceType("cq:Page")) {
    Resource template = resource.getChild("jcr:content/cq:template");
    if (template != null && "my/template".equals(template.adaptTo(String.class))) {
if (resource != null) {
    // we've found appropriate ancestor

SlingQuery is a tool that helps creating such queries in a more concise way. Above code could be written as:

import static$;
// ...

Dollar sign is a static method that takes the resource array and creates SlingQuery object. The closest() method returns the first ancestor matching the selector string passed as the argument.

SlingQuery is inspired by the jQuery framework. jQuery is the source of method names, selector string syntax and the dollar sign method used as a collection constructor.


  • useful operations to traverse the resource tree,
  • flexible filtering syntax,
  • lazy evaluation of the query result,
  • SlingQuery object is immutable (thread-safe),
  • fluent, friendly, jQuery-like API.


Add following Maven dependency to your pom.xml:



External resources