Re: Help: I cannot selectNodes with a simple XPath expression



On Apr 29, 4:56 am, suchenwi <richard.suchenwirth-
bauersa...@xxxxxxxxxxx> wrote:
On 28 Apr., 20:10, Hai Vu <wuh...@xxxxxxxxx> wrote:

set xml {<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://research.sun.com/wadl/2006/10";>
    <doc xmlns:jersey="http://jersey.dev.java.net/";
jersey:generatedBy="Jersey: 1.0.2 02/11/2009 07:45 PM"/>
    <resources base="https://test-server1:8844/restapp/";>
    </resources>
</application>
}
set doc [dom parse $xml]
set root [$doc documentElement]

# Why did this statement return an empty node?
set node [$root selectNodes /application/resources]

I suspect it's because of the anonymous namespace (xmlns=...) given in
the application tag. I tried:

% set xml <foo><bar>1</bar><bar>2</bar></foo>
<foo><bar>1</bar><bar>2</bar></foo>
% [dom parse $xml] documentElement root
domNode0114F27C
% $root selectNodes /foo/bar
domNode0114F2A8 domNode0114F2D4

So without an anonymous namespace, it works.

% set xml "<foo xmlns='/x/y'><bar>1</bar><bar>2</bar></foo>"
<foo xmlns='/x/y'><bar>1</bar><bar>2</bar></foo>
% [dom parse $xml] documentElement root
domNode0114F32C
% $root selectNodes /foo/bar
% $root selectNodes -namespaces {"" /x/y} /foo/bar
% $root selectNodes -namespaces { /x/y ""} /foo/bar

Even though I read the domNode man page, I could not figure out how to
set -namespaces that I'd again get the /foo/bar nodes...

You know, I think the problem is trying to use xsl or xml namespace
semantics in a tcl script. Tcl is a procedural language, but xml
parsing, xsl, whatever doesn't look at an element name as a string.
Instead an element has a namespace "prefix" a colon and an element
name. Tcl'ers tend to think in strings, where s:myelement =
s:myelement in every case. Unfortunately this isn't true in XML. The
truth is that "s" could represent any namespace at any point in a
document, depending on the attributes which look like "xmlns:xxx".

The question is: why the indirection? Simple: namespaces are unique.
If you map s to 'my:s:namespace' then you can write a script to look
for elements in the 'my:s:namespace' even if the document you are
parsing maps r = 'my:s:namespace'! Important? You decide.
.



Relevant Pages

  • Re: Cannot find root element in XML file
    ... If instead of querying the 'IncidentDefinitions' root element I use ... If those child or descendant elements are in the namespace ... Makes me wonder how I support both types of XML files, ...
    (microsoft.public.dotnet.xml)
  • Re: XML Validation error for namespace
    ... I have an xml file with a root, namespace definition and children. ... I need to create a schema to validate this xml: ...
    (microsoft.public.dotnet.framework)
  • Re: Help: I cannot selectNodes with a simple XPath expression
    ... % [dom parse $xml] documentElement root ...
    (comp.lang.tcl)
  • Re: ASP Coding Problem, XMLDOM-related (or just language-related!)
    ... incoming XML post request and respond to it. ... represents the single element that represents the root of the XML ... The problem is that documentElement doesn't seem to be returning Null, ...
    (microsoft.public.inetserver.asp.components)
  • Re: Namespace Issue with InnerXML
    ... inner XML document as is.x ... <root xmlns="http://pjc.BizTalk"</a>;> ... Sorry, your approach can't work, the namespace of an node in the DOM model is determined by its NamespaceURI property and you can't change that. ... You might want to look into XSLT to transform the XML. ...
    (microsoft.public.dotnet.xml)