Domanda

I am trying to run some Xquery over an XML file but every time I call the xmlObject.execQuery() it goes over the OutterXml and not in the InnerXml. Let me explain, I have this XML:

<test><MyXml><someValues><MoreNodes>Value1</MoreNodes></someValues></MyXml></test>

And this is my code:

XmlObject inputXml = XmlObject.Factory.parse(xmlFileContent).selectChildren(new QName("test"))[0];
    paramMap.put("var1", inputXml);

    options.setXqueryVariables(paramMap);

    XmlObject[] resultsObjects = xmlObject.execQuery("declare variable $var1 as element() external; <a>{$var1/someValues/MoreNodes}</a>", options);

But this returns <a/>.

If I change the XQuery to

declare variable $var1 as element() external; <a>{$var1/MyXml/someValues/MoreNodes}</a>

Then I get the result I am expecting: <a><MoreNodes>Value1</MoreNodes></a>

These are only tests that I am doing, but in reality the XQuery will be read from files that are compile and running on OSB so I cannot change the XQueries content to use the correct XPath.

Any idea on how to solve my problem?

BTW if I remove the code .selectChildren(new QName("test"))[0]; then the xmlObject.execQuery fails.

È stato utile?

Soluzione

After multiple tries I figure out that I can do something like:

<MyXml><someValues><MoreNodes>Value1</MoreNodes></someValues></MyXml>

*No need to insert the parent node <test> that I was inserting before.

and then do something like:

XmlObject inputXml = XmlObject.Factory.parse(xmlFileContent).selectChildren(new QName("MyXml"))[0];
paramMap.put("var1", inputXml);

options.setXqueryVariables(paramMap);

XmlObject[] resultsObjects = xmlObject.execQuery("declare variable $var1 as element() external; <a>{$var1/someValues/MoreNodes}</a>", options);

and with these two changes I got what I was looking for:

<a><MoreNodes>Value1</MoreNodes></a>

Hope this helps someone with the same issue that I was facing.There isn't a lot of documentation about how to use this classes.

Thanks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top