xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
- From: Wolfgang Hoschek <wolfgang.hoschek AT mac.com>
- To: peter murray-rust <pm286 AT cam.ac.uk>
- Cc: Elliotte Harold <elharo AT metalab.unc.edu>, xom-interest <xom-interest AT lists.ibiblio.org>
- Subject: Re: [XOM-interest] Scope of namespaces
- Date: Sun, 5 Feb 2006 16:03:44 -0800
Peter,
What you are doing is computing the namespaces declared *locally* on the given element (excluding it's ancestors). If you want the namespaces in scope, that is, including it's ancestors, you need to repeat what you are doing for the element and all its ancestors, and in doing so stuff the info into a set or map.
Like this:
private static Map getNamespacePrefixesInScope(Element
element) {
HashMap namespaces = new HashMap();
do {
int size =
element.getNamespaceDeclarationCount();
for (int i = 0; i < size; i++) {
String prefix =
element.getNamespacePrefix(i);
if (!namespaces.containsKey(prefix)) {
String uri =
element.getNamespaceURI(prefix);
namespaces.put(prefix, uri);
}
}
ParentNode parent = element.getParent();
element = (parent instanceof Element ?
(Element) parent : null);
} while (element != null);
return namespaces;
}
[which is the same as the non-public method Element.getNamespacePrefixesInScope()].
Wolfgang.
On Feb 5, 2006, at 3:44 PM, peter murray-rust wrote:
At 18:25 05/02/2006, Elliotte Harold wrote:
XOM's about as consistent on this as it's possible to be given the
general mess that is namespaces.
I agree that namespaces are one of the worst specs to come out of the
process. They were done in a great rush and generated much friction.
The use of "http://..." as a name rather than an address is probably
one of the worst ever decisions.
Given any element if you ask for the URI mapped to a given prefix,
then it will return the URI as specified by Namespaces in XML.
Agreed. But if I want to ask for the namespaceURIs in scope, rather
than the prefixes then I don't seem to be able to get them all:
If you think you've identified a case where this isn't true, then
please send in a sample that shows what XOM does report as the
namespace URI and what you think it should report instead.
Running under Xom1.12/Junit/Eclipse/Java 5.0:
public void testNamespaceScope() {
String s = "<a xmlns:ns='http://foo'><b foo='ns:bar'/></a>";
Document doc = null;
try {doc = new Builder().build(new
StringReader(s));} catch (Exception e) {}
Element a = doc.getRootElement();
System.out.println("NS: "+a.getNamespaceURI("ns"));
Element b = a.getFirstChildElement("b");
int nsCount = b.getNamespaceDeclarationCount();
System.out.println("NSCOUNT: "+nsCount);
for (int i = 0; i < nsCount; i++) {
System.out.println("NSPREFIX"+i+"
["+b.getNamespacePrefix(i)+"]");
System.out.println("NSURI"+i+"
["+b.getNamespaceURI(b.getNamespacePrefix(i))+"]");
}
System.out.println("NS1: "+b.getNamespaceURI("ns"));
}
gives:
NS: http://foo
NSCOUNT: 1
NSPREFIX0 []
NSURI0 []
NS1: http://foo
It seems that the prefix "ns" is not in scope as evidenced by the
namespace declaration count and there is no other means of iterating
through the namespaces (which is what I sometimes wish to do as I
want to see if a namespaceURI is in scope at that stage and I may not
know its prefix).
Thanks
P.
Peter Murray-Rust
Unilever Centre for Molecular Sciences Informatics
University of Cambridge,
Lensfield Road, Cambridge CB2 1EW, UK
+44-1223-763069
_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
-
[XOM-interest] Scope of namespaces,
peter murray-rust, 02/05/2006
-
Re: [XOM-interest] Scope of namespaces,
Elliotte Harold, 02/05/2006
-
Re: [XOM-interest] Scope of namespaces,
peter murray-rust, 02/05/2006
- Re: [XOM-interest] Scope of namespaces, Wolfgang Hoschek, 02/05/2006
-
Re: [XOM-interest] Scope of namespaces,
Elliotte Harold, 02/05/2006
- Re: [XOM-interest] Scope of namespaces, Michael Kay, 02/06/2006
-
Re: [XOM-interest] Scope of namespaces,
Elliotte Harold, 02/05/2006
-
Re: [XOM-interest] Scope of namespaces,
Michael Kay, 02/06/2006
- Re: [XOM-interest] Scope of namespaces, Elliotte Harold, 02/06/2006
- Re: [XOM-interest] Scope of namespaces, Elliotte Harold, 02/06/2006
- Re: [XOM-interest] Scope of namespaces, Steve Loughran, 02/06/2006
-
Re: [XOM-interest] Scope of namespaces,
Michael Kay, 02/06/2006
-
Re: [XOM-interest] Scope of namespaces,
Steve Loughran, 02/06/2006
- Re: [XOM-interest] Scope of namespaces, Elliotte Harold, 02/06/2006
- Re: [XOM-interest] Scope of namespaces, Elliotte Harold, 02/06/2006
-
Re: [XOM-interest] Scope of namespaces,
peter murray-rust, 02/05/2006
-
Re: [XOM-interest] Scope of namespaces,
Elliotte Harold, 02/05/2006
Archive powered by MHonArc 2.6.24.