Re: Help: I cannot selectNodes with a simple XPath expression
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Wed, 29 Apr 2009 17:01:48 -0700 (PDT)
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.
.
- Follow-Ups:
- References:
- Prev by Date: Re: Bug XOTcl ??
- Next by Date: Re: Help: I cannot selectNodes with a simple XPath expression
- Previous by thread: Re: Help: I cannot selectNodes with a simple XPath expression
- Next by thread: Re: Help: I cannot selectNodes with a simple XPath expression
- Index(es):
Relevant Pages
|