How to use an xPath to designate an element regardless of Namespace.

How to use an xPath to designate an element regardless of Namespace.

Summary:

An xml being referenced may designate 1 or more Namespaces used. This can be difficult to reference if you are attempting to look for a specific element but are unsure of the Namespace used.

 

Description:

Using a wild card (*) along with specifying local-name will return a result regardless of namespace.

 

Example:

This xpath will specify /Project/PropertyGroup/IncludePath from the  Sample XML, regardless of the xml namespace given:

/*[local-name()='Project']/*[local-name()='PropertyGroup']/*[local-name()='IncludePath']

 

Sample XML:


<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <PropertyGroup>
          <IncludePath>SampleValue</IncludePath>
     </PropertyGroup>
</Project>

 

Additional Info:

It is worth noting, additional xpath syntax may be needed if the elements appear more than once.

 

Here is some info on namespaces:

https://www.w3schools.com/xml/xml_namespaces.asp

Labels (1)
Was this article helpful? Yes No
No ratings
Version history
Revision #:
3 of 3
Last update:
‎Jul 22, 2020 03:13 PM
Updated by:
 
Contributors