Skip to main content
Inspiring
March 24, 2022
Answered

evaluateXPathExpression attributes of element with namespace

  • March 24, 2022
  • 1 reply
  • 596 views

I have an xml like this (reduced example):

<root xmlns:test="http://ns.test.com/test/2.0">
    <test:master>
        <test:graphic control="ABC"/>
        <test:textframe>TEXT</test:textframe> 
    </test:master>

    <test:master>
        <test:graphic control="XYZ"/>
        <test:textframe>TEXT</test:textframe> 
    </test:master>
</root>

As you can see the "test:graphic" element contains a "control" attribute.

I'd like to get all elements with a "control" attribute of the first "master" element with evaluateXPathExpression.

 

WITHOUT namespaces this code works:

var root = doc.xmlElements[0]; // doc is the current document
var xmlElements = root.xmlElements[0].evaluateXPathExpression("//*[@control]");
--> 1 result

 

But WITH namespaces it doesn't:

var namespaces = new Array(new Array("test","http://ns.test.com/test/2.0"));
var root = doc.xmlElements[0]; // doc is the current document
var xmlElements = root.xmlElements[0].evaluateXPathExpression("//*[@control]",namespaces);
--> 0 results

 

Althoug it works WITH namespaces on the root element (not on the first "master" like above) 

var namespaces = new Array(new Array("test","http://ns.test.com/test/2.0"));
var root = doc.xmlElements[0]; // doc is the current document
var xmlElements = root.evaluateXPathExpression("//*[@control]",namespaces); //
--> 2 results

 

So i was wondering what am i doing wrong and how adress only a specific "master" element.

This topic has been closed for replies.
Correct answer patMorita

I think i found a solution:

 

root.evaluateXPathExpression("//test:master[1]/*[@control]",namespaces);

 

1 reply

Legend
March 25, 2022

Try "//*[@*[local-name()='control']]"

 

patMoritaAuthor
Inspiring
March 25, 2022

Hi Dirk

 

Thank you for your help!

It works if i adress the root

 

 

var namespaces = new Array(new Array("test","http://ns.test.com/test/2.0"));
var root = doc.xmlElements[0]; // doc is the current document
var xmlElements = root.evaluateXPathExpression("//*[@*[local-name()='control']]",namespaces); //
--> 2 results

 

 

 

But it doesn't if i adress a child of root (what i'm looking for)

 

 

var namespaces = new Array(new Array("test","http://ns.test.com/test/2.0"));
var root = doc.xmlElements[0]; // doc is the current document
var xmlElements = root.xmlELements[0].evaluateXPathExpression("//*[@*[local-name()='control']]",namespaces); //
--> 0 results

 

 

  

Maybe there's another approach? Is there a possibility to adress the n'th element of root like so:

 

 

root.evaluateXPathExpression("//test:master[n]/*[@control]",namespaces)

 

 

 

patMoritaAuthorCorrect answer
Inspiring
March 25, 2022

I think i found a solution:

 

root.evaluateXPathExpression("//test:master[1]/*[@control]",namespaces);