Operations Orchestration Support and News Forum
Showing results for 
Search instead for 
Do you mean 

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

Occasional Contributor

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


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?



Petr Panuska


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 = [];
st.push( ch & 0xFF );
ch = ch >> 8;
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.





HP Software OO RnD, Community Assistance Team, https://hpln.hp.com/group/operations-orchestration