public final class XPathHelper
extends java.lang.Object
XPaths are required to be namespace-aware. Typically this is done by using
a namespace-prefixed query, with the actual namespace URI being set in the
context of the XPath expression. This is not possible to express using a
simple String return value. As a work-around, this method uses a potentially
slower, but more reliable, mechanism for ensuring the correct namespace
context is selected. The mechanism will appear like (for Elements):
.../*[local-name() = 'tag' and namespace-uri() = 'uri']
Similarly, Attributes will have a syntax similar to:
.../@*[local-name() = 'attname' and namespace-uri() = 'uri']
This mechanism makes it possible to have a simple namespace context, and a
simple String value returned from the methods on this class.
This class does not provide ways to access document-level content. Nor does it provide ways to access data relative to the Document level. Use absolute methods to access data from the Document level.
The methods on this class assume that the Document is above the top-most Element in the XML tree. The top-most Element is the one that does not have a parent Element (although it may have a parent Document). As a result, you can use Element data that is not attached to a JDOM Document.
Detached Attributes, and detached non-Element content are not treated the same. If you try to get an Absolute path to a detached Attribute or non-Element Content you will get an IllegalArgumentException. On the other hand it is legal to get the relative XPath for a detached node to itself ( but to some other node will cause an IllegalArgumentException because the nodes do not share a common ancestor).
Note: As this class has no knowledge of the document content, the generated XPath expression rely on the document structure. Hence any modification of the structure of the document may invalidate the generated XPaths.
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getAbsolutePath(Attribute to)
Returns the absolute path to the specified to Content.
|
static java.lang.String |
getAbsolutePath(Content to)
Returns the absolute path to the specified to Content.
|
static java.lang.String |
getRelativePath(Attribute from,
Attribute to)
Returns the relative path from the given from Attribute to the specified
to Attribute as an XPath expression.
|
static java.lang.String |
getRelativePath(Attribute from,
Content to)
Returns the relative path from the given from Attribute to the specified
to Content as an XPath expression.
|
static java.lang.String |
getRelativePath(Content from,
Attribute to)
Returns the relative path from the given from Content to the specified to
Attribute as an XPath expression.
|
static java.lang.String |
getRelativePath(Content from,
Content to)
Returns the relative path from the given from Content to the specified to
Content as an XPath expression.
|
public static java.lang.String getRelativePath(Content from, Content to)
from
- the Content from which the the generated path shall be applied.to
- the Content the generated path shall select.java.lang.IllegalArgumentException
- if to
and from
are not part of the same
XML treepublic static java.lang.String getRelativePath(Content from, Attribute to)
from
- the Content from which the the generated path shall be applied.to
- the Attribute the generated path shall select.java.lang.IllegalArgumentException
- if to
and from
are not part of the same
XML treepublic static java.lang.String getRelativePath(Attribute from, Attribute to)
from
- the Attribute from which the the generated path shall be applied.to
- the Attribute the generated path shall select.java.lang.IllegalArgumentException
- if to
and from
are not part of the same
XML treepublic static java.lang.String getRelativePath(Attribute from, Content to)
from
- the Attribute from which the the generated path shall be applied.to
- the Content the generated path shall select.java.lang.IllegalArgumentException
- if to
and from
are not part of the same
XML treepublic static java.lang.String getAbsolutePath(Content to)
to
- the Content the generated path shall select.java.lang.IllegalArgumentException
- if to
is not an Element and it is detached.public static java.lang.String getAbsolutePath(Attribute to)
to
- the Content the generated path shall select.java.lang.IllegalArgumentException
- if to
is detached.Copyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.