java123999
java123999

Reputation: 7394

XQuery is returning "false" when false is not a field in XML?

I have the following XML:

<personDetails>
    <person>
        <name>Mr Test</name>
        <paid>Yes</paid>
        <details>
            <detail>
                <id>12345</id>
                <address>15 Sample Street</address>
            </detail>
        </details>
    </person>
</personDetails>

I am trying to returning the value for paid when given a certain name and ID. The value being returned is "false" which is incorrect and not even in my XML.

Is my current query / java method correct?

Current Query:

//personDetails/person[name='Mr Test'] and //personDetails/person/details/detail[id ='S0335171']/paid/text()

Current method:

public String getPaidByNameAndId(String name, String id) {
        final String _Name = name;
        final String _id = id;
        return engine.new Query<String>(PERSON_COLLECTION) {

            @Override
            protected String query(Collection collection) throws Exception {
                XQueryService service = queryService();

                ResourceSet resourceSet = service.query(
                        format("//personDetails/person[name='%s'] and //personDetails/person/details/detail[id ='%s']" +
                                        "/paid/text()"
                                , StringEscapeUtils.escapeXml(_Name),
                                StringEscapeUtils.escapeXml(_id)
                        ));

                for (String resource : new IterableStringResources(resourceSet)) {
                    results.add(resource);
                }
                return results.get(0);
            }
        }.execute();
    }

For reference, the xQueryService I am using is:

org.exist.xmldb.XQueryService;

Upvotes: 1

Views: 202

Answers (1)

har07
har07

Reputation: 89285

Your entire XPath/XQuery is indeed returning a boolean value (two values combined using and operator). The correct XPath/XQuery according to your explanation would be :

//personDetails/person[name='Mr Test' and details/detail[id ='S0335171']]/paid/text()

xpath demo

Upvotes: 3

Related Questions