For a developer normally when they hear about Xpath they think about the expression to select nodes in XML document because as per the W3C, XPath is a major element in W3C’s XSLT standard and as per Wikipedia also XPath, the XML Path Language, is a query language for selecting nodes from an XML document. Now let’s get in to the world of Alfresco.
What is the significance of Xpath query in alfresco?
It is type of query supported by alfresco which helps to find particular node in a repository. So, if you want to search for particular node which is residing at known path or location we can use this xpath query in code to get the node of that content and once we get the node we have total control of that content.
What is the syntax of Xpath?
XPath expressions are executed in the context of a given node – so that relative xpath expressions can be evaluated. The store root node is “/”.
So, it always start with main folder and then drill down to perticular node through that path. You just need to be aware about the name of those parent folders and namespace prefix resolver
Easy syntax so convenient to create query.
While it is simpler and convenient at the same time we need to be aware that it will not be that much well performing as it uses nodeService internally to drill down to particular node with repository just like in Xpath expression is used to search through XML DOM Model.
How to use it in development?
Both NodeService and SearchService has APIs which can be used to utilize this query.
/** * Select nodes using an xpath expression. * @param contextNode - the context node for relative expressions etc * @param XPath - the xpath string to evaluate * @param parameters - parameters to bind in to the xpath expression * @param namespacePrefixResolver - prefix to namespace mappings * @param followAllParentLinks - if false ".." follows only the primary parent links, if true it follows all * @return a list of all the child assoc relationships to the selected nodes */ public List<ChildAssocRef> selectNodes(NodeRef contextNode, String XPath, QueryParameterDefinition parameters, NamespacePrefixResolver namespacePrefixResolver, boolean followAllParentLinks);
public ResultSet query(StoreRef stores, String language, String query, StringqueryOptions)
In above API you need to pass Language as Xpath and in place of query you can put your xpath query.
There are many more apis which support this but this should give you good starting point. Once you explore this you can try with advance functions and xpath queries supported in alfresco.