1
votes

I am currently using EclipseLink MOXy 2.4.1 and trying to update an element's value via XPaths. The input used is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<expenseReport>
    <user>
        <userName>Sanaulla</userName>
    </user>
    <items>
        <item>
            <itemName>Seagate External HDD</itemName>
            <purchasedOn>August 24, 2010</purchasedOn>
            <amount>6776.5</amount>
        </item>
        <item>
            <itemName>External HDD</itemName>
            <purchasedOn>August 24, 2010</purchasedOn>
            <amount>677336.5</amount>
        </item>
    </items>
</expenseReport>

The XSD is

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="expenseReport" type="ExpenseT"/>
    <xs:complexType name="ExpenseT">
        <xs:sequence>
            <xs:element name="user" type="UserT"/>
            <xs:element name="items" type="ItemListT"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="UserT">
        <xs:sequence>
            <xs:element name="userName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ItemListT">
        <xs:sequence>
            <xs:element name="item" type="ItemT" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ItemT">
        <xs:sequence>
            <xs:element name="itemName" type="xs:string"/>
            <xs:element name="purchasedOn" type="xs:string"/>
            <xs:element name="amount" type="xs:decimal"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

And my Java code is :

JAXBContext context = JAXBContext.newInstance("sample.jaxb.xsd");
JAXBElement<ExpenseT> element = (JAXBElement<ExpenseT>)unmarshaller.unmarshal(new File("resources/sample_before.xml"));
ExpenseT expenseReport = element.getValue();
context.setValueByXPath(expenseReport, "items/item[1]/amount/text()", null, "100.11");
/** Marshalling code **/

I have my java classes created from the XSD using Moxy's tool "jaxb-compiler.cmd". As you can see from the above code, I am trying to set the amount of 1st item to "100.11". But it doesn't set the value, nor does it throw any exception. I tried setting the username above using XPath like this

context.setValueByXPath(expenseReport, "user/userName/text()", null, "ABC");

And, it does set/update the value properly. Can you please advise on how can I set values for complex XPaths with Predicates? Something like this :

items/item[itemName='Seagate External HDD']/amount
1

1 Answers

0
votes

We recently fixed a bug in our EclipseLink 2.4.2 and 2.5.0 streams related to setting values in which the XPath contains a postional indicator (ie "foo[1]/bar[2]/text()"). You can download a nightly build from the following link:

You can use predicates in mappings with @XmlPath (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html), but we currently don't supports this with our setValueByXPath call. Could you open an enhancement