Xquery returns result for one set of elements, but not for others?

I have the following XML:

<Details>
    <Detail>
        <name>Detail one</name>
        <id>001</id>
    </Detail>
    <Detail>
        <name>Detail two</name>
        <id>002</id>
    </Detail>
</Details>

I request this by name to get the identifier using a service XQuerywith a tag Java:

 public String getIdByName(String detailName) {
        final String detail_Name = detailName;
        return engine.new Query<String>(DETAILS_COLLECTION) {

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

                ResourceSet resourceSet = service.query(
                        format("//Details/Detail[name='%s']" +
                                        "/id/text()"
                                , StringEscapeUtils.escapeXml(detailName)

                        ));

                List<String> ids = newArrayList();

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

This method correctly returns the identifier for this section XMLnamed "Detail One":

<Detail>
    <name>Detail one</name>
    <id>001</id>
<Detail>

But it returns no values ​​for the second named "Detail two":

<Detail>
    <name>Detail two</name>
    <id>002</id>
</Detail>

What could be the reason for this?

+4
source share
2 answers

Nothing in your code looks incorrect, so I would suspect your input. You can try to sanitize it before running the query:

"//Details/Detail[name=fn:normalize-space('%s')]" 

. , .

+3

, , :

return ids.get(0);

, 0.

:

return ids;

, .

, .

0

Source: https://habr.com/ru/post/1660526/


All Articles