How to manage XML namespaces (xmlns) in XPath result filter? (146 Views)
Reply
Occasional Contributor
Petr_Panuska
Posts: 2
Registered: ‎11-23-2010
Message 1 of 2 (146 Views)

How to manage XML namespaces (xmlns) in XPath result filter?

Hello,

I'm using XPath result filter to obtain an attribute value from a returned XML document. The trouble is that the returned document contains definition of the default namespace (xmlns attribute in the root element). According to XPath spec, unprefixed names are in 'no namespace'; to reference elements from a defined namespace, the namespace must be registered first and then use the namespace prefix when referencing the namespaced elements.

 

I'm trying to reference this attribute in the attached XML: /server/image/@id

 

However, this XPath does not work (unsurprisingly, as I did not register the namespace ""http://docs.openstack.org/compute/api/v1.1").

 

The only workaround I found out so far is to use this XPath instead: /server/*[name()='image']/@id

 

I don't like the workaround and would rather use the clean solution - register the namespace; but how to do it in OO?

 

Thanks,

Petr Panuska

Advisor
EF
Posts: 22
Registered: ‎04-06-2010
Message 2 of 2 (125 Views)

Re: How to manage XML namespaces (xmlns) in XPath result filter?

I couldnt find an easy solution to your request. 

 

It is possible to use Java via JavaScript (since OO uses Rhino) to use the Java XPath capabilities. 

 

You can create a scriptlet with two inputs "xmlInput", "xpathExpr". And then give it this code:

function stringToBytes(str){
var ch, st, re = [];
for(var i = 0; i < str.length; i++){
ch = str.charCodeAt(i);
st = [];
do{
st.push( ch & 0xFF );
ch = ch >> 8;
}
while(ch);
re = re.concat(st.reverse());
}
return re;
}

var dbFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
var dBuilder = dbFactory.newDocumentBuilder();
var doc = dBuilder.parse(new java.io.ByteArrayInputStream(stringToBytes(xmlInput)));
var xPathfactory = javax.xml.xpath.XPathFactory.newInstance();
var xpath = xPathfactory.newXPath();

scriptletResponse = "success";
scriptletResult = xpath.evaluate(xpathExpr, doc);

 

 

We will look at improving this in a future version of OO.

 

HTH,

Emil

 

HP Software OO RnD, Community Assistance Team, https://hpln.hp.com/group/operations-orchestration
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.