Search code examples
xpathosb

Xpath Expression evaluation on attributes with any namespace prefix


Could you please help me on this xpath expression evaluation

I am working on fetching the proxy references. In the xml file the references will get stored as:

One way of XML file will have the reference as below:

con1:service ref="MyProject/ProxyServices/service1" 
xsi:type="con2:PipelineRef" xmlns:ref="http://www.bea.com/wli/sb/reference"/

here in the xml file the name spaces are:

xmlns:con1="http://www.bea.com/wli/sb/stages/config"
xmlns:con2="http://www.bea.com/wli/sb/pipeline/config"

Another way of XML will have the reference as below.

con1:service ref="MyProject/ProxyServices/service2" 
xsi:type="ref:ProxyRef" xmlns:ref="http://www.bea.com/wli/sb/reference"/

here in the xml file the name spaces are:

xmlns:con1="http://www.bea.com/wli/sb/stages/config"
xmlns:ref="http://www.bea.com/wli/sb/reference"

I have used this xpath expression, this is not fetching the reference service values, could you please help what is wrong in it.

"//service[@type= @*[local-name() ='ProxyRef' or  @type=@*[local-name() ='PipelineRef']]/@ref"

when I used like this it is working but, name space prefix is keep on changes when there are multiple references in the xml file.

"//service[@type='ref:ProxyRef'or @type='con:PipelineRef' or @type='con1:PipelineRef' or @type='con2:PipelineRef' or @type='con3:PipelineRef' ...@type='con20:PipelineRef' ]/@ref"; 

Now here basically the type attribute PipelineRef is keep on changing the name space prefix from con to con(n). Now I am looking for something which supports some thing like @type='*:PipelineRef' or @type='con*:PipelineRef' or the best way to fetch the service element reference attribute value.

Thanks in advance.


Solution

  • Try using contains() like so :

    //service[contains(@type,':ProxyRef') or contains(@type,':PipelineRef')]
    

    Another alternative would be using ends-with() function which is more precise for this purpose compared to contains() function. However, ends-with() isn't available in xpath 1.0, so there is a chance that you need to implement it yourself (feasible, but the xpath result is less intuitive for me).