how to ignore namespaces with XPath
Asked Answered
N

5

139

My goal is to extract certain nodes from multiple XML files with multiple namespaces using XPath. Everything works fine as long as I know the namespace URIs. The namespace name itself remains constant, but the Schemas (XSD) are sometimes client-generated i.e. unknown to me. Then I am left with basically three choices:

  1. Use just one schema for the namespace, hoping nothing goes wrong (can I be sure?).
  2. Get the children nodes of the document and look for the first node with a namespace URI, hoping it's there and just use the URI, hoping it's the correct one. This can go wrong for multiple reasons
  3. Somehow tell xpath : "look, I don't care about the namespaces, just find ALL nodes with this name, I can even tell you the name of the namespace, just not the URI". And this is the question here...

This is not a reiteration of numerous "my xpath expression doesn't work because I am not aware of namespace awareness" questions as found here or here. I know how to use namespace awareness, just not how to get rid of it.

Nernst answered 14/12, 2010 at 14:57 Comment(6)
If you don't know the schemas, how do you know what elements you want?Gunnel
Exact duplicate of How to ignore namespace when parsing XML document with XPathUla
thank you for pointing out, Alejandro. The search for "ignore namespace xpath" should have revealed this one, but it didntNernst
@Paul Butcher - i dont know the exact URI of the schemas but the changes betweeen the schemas (rather versions of one schema) luckily dont affect the nodes i'm looking for.Nernst
@kostja: Don't search with SO search box, it's useless... Try Google next time. In fact, this is encouraged by SO team.Ula
Google sitesearch actually does a better job at finding useful stuff on SO. I wonder why its not an option per default. Thanks again, AlejandroNernst
D
209

You can use the local-name() XPath function. Instead of selecting a node like

/path/to/x:somenode

you can select all nodes and filter for the one with the correct local name:

/path/to/*[local-name() = 'somenode']
Devito answered 14/12, 2010 at 15:5 Comment(3)
You may also use the local-name() to refer to attributes as well, in a namespace-unaware manner, see: https://mcmap.net/q/168188/-xpath-how-to-identify-an-attribute-in-a-predicate-in-a-namespace-unaware-way-duplicate/274677Folder
Take a look at this tutorial: codesimplify.com/java/java-xpath-ignore-namespace-exampleJataka
The codesimplify link by @Jataka is dead. Here is the wayback version: web.archive.org/web/20210624020506/http://www.codesimplify.com/…Scots
F
30

You can do the same In XPath2.0 in a less verbose syntax:

/path/to/*:somenode
Fingering answered 21/2, 2017 at 8:41 Comment(1)
This would not work with elements in the default namespace, though.Bruise
H
12

Or you can use name():

/path/to/*[name() = 'somenode']

Or only search attributes:

//*[@attribute="this one"]

If you open the xml as a powershell object, it ignores the namespaces:

[xml]$xml = get-content file.xml
$xml.path.to.somenode
Haemachrome answered 23/3, 2020 at 17:14 Comment(1)
Also works with //*[name() = 'somenode']Speos
G
4

You could use Namespace = false on a XmlTextReader

[TestMethod]
public void MyTestMethod()
{
    string _withXmlns = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ParentTag xmlns=""http://anyNamespace.com"">
<Identification value=""ID123456"" />
</ParentTag>
";

    var xmlReader = new XmlTextReader(new MemoryStream(Encoding.Default.GetBytes(_withXmlns)));

    xmlReader.Namespaces = false;

    var content = XElement.Load(xmlReader);

    XElement elem = content.XPathSelectElement("/Identification");

    elem.Should().NotBeNull();
    elem.Attribute("value").Value.Should().Be("ID123456");
}

with :

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Gabriello answered 19/3, 2018 at 10:54 Comment(1)
For selecting a node through XPath, this works; Unfortunately you are not able to save the document due to a 'The 'xmlns' attribute is bound to the reserved namespace error.Lothar
R
0

It is my example in Qt C++. Qt supports XPath 2.0:

    QString planePath = ":/Models/Plane.dae";
    QFile f(planePath);
    if (!f.open(QIODevice::ReadOnly))
    {
        std::cerr << "Failed to load the file: " <<
                     planePath.toStdString() << std::endl;
        return;
    }

    QXmlQuery query;
    query.bindVariable("myFile", &f);
//    query.setQuery("doc($myFile)//*[local-name() = 'p']/text()"); // it works too but it is XPath 1.0
    query.setQuery("doc($myFile)//*:p/text()");

    QString result;
    query.evaluateTo(&result);
    qDebug() << result;
    f.close();

Program output: "1 0 0 2 0 1 0 0 2 1 0 3 3 0 4 2 0 5\n"

Plane.dae

<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <asset>
    <contributor>
      <author>Blender User</author>
      <authoring_tool>Blender 2.83.3 commit date:2020-07-22, commit time:06:01, hash:353e5bd7493e</authoring_tool>
    </contributor>
    <created>2020-08-03T14:03:19</created>
    <modified>2020-08-03T14:03:19</modified>
    <unit name="meter" meter="1"/>
    <up_axis>Z_UP</up_axis>
  </asset>
  <library_effects>
    <effect id="PlaneMaterial-effect">
      <profile_COMMON>
        <technique sid="common">
          <lambert>
            <emission>
              <color sid="emission">0 0 0 1</color>
            </emission>
            <diffuse>
              <color sid="diffuse">0.01664001 0.8000001 0.01191879 1</color>
            </diffuse>
            <reflectivity>
              <float sid="specular">0.5</float>
            </reflectivity>
          </lambert>
        </technique>
      </profile_COMMON>
    </effect>
  </library_effects>
  <library_images/>
  <library_materials>
    <material id="PlaneMaterial-material" name="PlaneMaterial">
      <instance_effect url="#PlaneMaterial-effect"/>
    </material>
  </library_materials>
  <library_geometries>
    <geometry id="Plane-mesh" name="Plane">
      <mesh>
        <source id="Plane-mesh-positions">
          <float_array id="Plane-mesh-positions-array" count="12">-1 -1 0 1 -1 0 -1 1 0 1 1 0</float_array>
          <technique_common>
            <accessor source="#Plane-mesh-positions-array" count="4" stride="3">
              <param name="X" type="float"/>
              <param name="Y" type="float"/>
              <param name="Z" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <source id="Plane-mesh-normals">
          <float_array id="Plane-mesh-normals-array" count="3">0 0 1</float_array>
          <technique_common>
            <accessor source="#Plane-mesh-normals-array" count="1" stride="3">
              <param name="X" type="float"/>
              <param name="Y" type="float"/>
              <param name="Z" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <source id="Plane-mesh-map-0">
          <float_array id="Plane-mesh-map-0-array" count="12">1 0 0 1 0 0 1 0 1 1 0 1</float_array>
          <technique_common>
            <accessor source="#Plane-mesh-map-0-array" count="6" stride="2">
              <param name="S" type="float"/>
              <param name="T" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <vertices id="Plane-mesh-vertices">
          <input semantic="POSITION" source="#Plane-mesh-positions"/>
        </vertices>
        <triangles material="PlaneMaterial-material" count="2">
          <input semantic="VERTEX" source="#Plane-mesh-vertices" offset="0"/>
          <input semantic="NORMAL" source="#Plane-mesh-normals" offset="1"/>
          <input semantic="TEXCOORD" source="#Plane-mesh-map-0" offset="2" set="0"/>
          <p>1 0 0 2 0 1 0 0 2 1 0 3 3 0 4 2 0 5</p>
        </triangles>
      </mesh>
    </geometry>
  </library_geometries>
  <library_visual_scenes>
    <visual_scene id="Scene" name="Scene">
      <node id="Plane" name="Plane" type="NODE">
        <matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
        <instance_geometry url="#Plane-mesh" name="Plane">
          <bind_material>
            <technique_common>
              <instance_material symbol="PlaneMaterial-material" target="#PlaneMaterial-material">
                <bind_vertex_input semantic="UVMap" input_semantic="TEXCOORD" input_set="0"/>
              </instance_material>
            </technique_common>
          </bind_material>
        </instance_geometry>
      </node>
    </visual_scene>
  </library_visual_scenes>
  <scene>
    <instance_visual_scene url="#Scene"/>
  </scene>
</COLLADA>
Rabideau answered 6/8, 2020 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.