Search code examples
javaxpathjaxbimplementationmoxy

JAXB / MOXy: How to set which xpath impl to use?


I use MOXy's @XmlPath("/foo/bar[@baz]").

Elsewhere in the app I need XSLT 2.0 so I included Saxon HA 9.5 on my classpath. But that caused the XPath fail (doesn't find the nodes, unlike JDK's implementation).

How can I set for MOXy which XML / XPath impl to use?

Currently I use jaxb.properties next to JAXB bean's .class, but I'm okay to create the marshaller with some factory options or such.

Bonus question: I think the problem with XPath is that the XML file uses namespaces, <deployment xmlns="urn:jboss:bean-deployer:2.0"> ...
How can I make Saxon ignore namespaces in XPath? (without rewriting the XPath expression).


Solution

  • EclipseLink JAXB (MOXy)'s @XmlPath annotation supports a subset of the XPath specification. The XPath handling is done by MOXy itself. The following concepts are supported:

    • Attribute - @id
    • Element - address
    • Element by Position - address[1]
    • Element by Predicate - address[@type='mailing']
    • Element Text - name/text()
    • Text - text()
    • Self - .
    • Combination - personal-info/name[2]/text()

    For namespace qualified nodes, the prefixes defined in the @XmlNs annotations can be used to qualify the XPath fragments. Unqualified fragments will assumed to be in the namespace specified using @XmlSchema.