Search code examples
eclipselinkmarshallingmoxy

EclipseLink MOXy @XmlPath is returning the incorrect data for contains


I am attempting to unmarshal an XML data stream with EclipseLink MOXy with an XPath using the contains function.

When I apply the sample XPath directly to the sample XML data stream I get the correct value returned (Ref_number_1). However, when I unmarshal this using MOXy, the value that is set for refNumber1 is "Ref_number_2".

Does MOXy not support this type of XPath? It would appear that it at least is understanding it because it's not throwing an error, just setting the wrong value.

Anyone have any experience with this sort of thing? Know of a better approach?

Thanks for any help.

Marshal code:

String s = //xml stream from restful service (see xml example below);
StringReader sr = new StringReader(s);
ReferenceNumber refNum = (ReferenceNumber)marshaller.unmarshal(
            new StreamSource(sr));

Member annotation:

@XmlPath("Header/ReferenceNumbers/ReferenceNumber[contains(ReferenceNumberType, \"REF_NUMBER_TYPE_1\")]/ReferenceNumber/text()")
private String refNumber1;

XML data:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
  <Header>      
    <ReferenceNumbers>
      <ReferenceNumber>
        <ReferenceNumber>Ref_number_1</ReferenceNumber>
        <ReferenceNumberType>REF_NUMBER_TYPE_1</ReferenceNumberType>
      </ReferenceNumber>
      <ReferenceNumber>
        <ReferenceNumber>Ref_number_2</ReferenceNumber>
        <ReferenceNumberType>REF_NUMBER_TYPE_2</ReferenceNumberType>
      </ReferenceNumber>
    </ReferenceNumbers>
  </Header>
</Document>

Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    Currently MOXy does not support XPaths of that form. Currently a conditional check must be on an attribute contained within the element such as (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html).

    @XmlPath("personal-info/name[@type='first']/text()")
    String firstName;
    

    I have entered the following bug so that we improve the validation that we do on our @XmlPath annotation:

    I am also interested in the use case described in your question. Would you mind entering an enhancement requires against the MOXy component for this: