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

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 """).


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

Posts: 25
Registered: ‎04-06-2010
Message 2 of 2 (338 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 = [];
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;
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,
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.