<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.0.9">
</HEAD>
<BODY>
You really need knowlege in your application as to the namespace URIs - I take the point that you have URIs that are changing, but it is the URI and <B>not</B> the prefix that is important in XML, indeed the prefix is essentially irrelevant. Prefixes are purely for brevity and convenience in XML syntax. The actual prefix used can be changed by the user without impacting the underlying result. It is very, very bad practice to assume specific prefixes (this goes totally against the XML ethos).<BR>
<BR>
You can do something like:<BR>
<BR>
private static final Properties URIS = new Properties();<BR>
<BR>
static {<BR>
URIS.load(getClass().getResourceAsStream("uris.properties"));<BR>
}<BR>
<BR>
private static final String MODS_URI = <A HREF="http://www.loc.dov/mods/v3">URIS.</A>getProperty("mods");<BR>
private static final String MIX_URI = URIS.getProperty("mix");<BR>
<BR>
private static final Namespace MODS = Namespace.getNamespace("totallyArbitraryPrefixDoesNotHaveToMatchTheDocumentButIsUniqueInThisDocumentsSetOfPrefixes", MODS_URI);<BR>
private static final Namespace MIX = Namespace.getNamespace("anotherTotallyArbitraryPrefixDoesNotHaveToMatchTheDocumentButIsUniqueInThisDocumentsSetOfPrefixes", MIX_URI);<BR>
<BR>
...<BR>
<BR>
and to access the required attribute in the MODS namespace from the required element in the MIX namespace:<BR>
<BR>
doc.getRootElement().getChild("a", MIX).getAttribute("x", MODS);<BR>
<BR>
Better still, instead of using the JDOM API directly, use an XPath of the form "prefixmix:a/@prefixmods:x" initialized using the two required namespaces (clearly using nice, short XPath-scope unique prefixes), executed against the root element.<BR>
<BR>
Hope that's helpful!<BR>
<BR>
Phil :n)<BR>
<BR>
PS: You would put the file "uris.properties" in the class's package and populate it like:<BR>
<BR>
# Define the URIs used in the code. The key is not the prefix<BR>
# used in the XML document or document accessing code<BR>
# but is just a meaningful key for accessing the URI definitions<BR>
mods=<A HREF="http://www.loc.gov/mods/v3">http://www.loc.gov/mods/v3</A><BR>
mix=<A HREF="http://www.loc.gov/mix/">http://www.loc.gov/mix/</A><BR>
<BR>
On Thu, 2004-02-05 at 08:58, Frédéric Laurent wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE><FONT COLOR="#737373"><I>Quoting Garey Mills <gmills@library.berkeley.edu>:
> Hi -
Hello
>
>         I am using JDOM to parse documents that use attributes in a number
> of different namespaces. The namespaces correspond to standards that are
> being revised, so I can't be sure what their URI is (since each revision
> is given a new URI).
>
>         The namespaces are declared in the root element, for example:
>
> xmlns:mets="</FONT><A HREF="http://www.loc.gov/METS/"><U>http://www.loc.gov/METS/"</U></A>
<FONT COLOR="#737373">> xmlns:moa2="</FONT><A HREF="http://sunsite.berkeley.edu/MOA2/"><U>http://sunsite.berkeley.edu/MOA2/"</U></A>
<FONT COLOR="#737373">> xmlns:mods="</FONT><A HREF="http://www.loc.gov/mods/v3"><U>http://www.loc.gov/mods/v3"</U></A>
<FONT COLOR="#737373">> xmlns:mix="</FONT><A HREF="http://www.loc.gov/mix/"><U>http://www.loc.gov/mix/"</U></A>
<FONT COLOR="#737373">> xmlns:xlink="</FONT><A HREF="http://www.w3.org/TR/xlink"><U>http://www.w3.org/TR/xlink"</U></A>
<FONT COLOR="#737373">> xmlns:xsi="</FONT><A HREF="http://www.w3.org/2001/XMLSchema-instance"><U>http://www.w3.org/2001/XMLSchema-instance"</U></A>
<FONT COLOR="#737373">>
>
>         If, at some point in the document, I want to retrieve an attribute
> that belongs to, say, the 'mods' namespace, can I just use the prefix?
>
>
>         Attribute x = myElement.getAttribute("x", "mods");
>
>         I don't think I can because getAttribute needs a Namespace and not
> a string
>
>         So do I have to do something more convoluted, such as retrieve the
> namespaces defined on the element, search for the one with the name "mods"
> and then include that one in the get Attribute call?
>
>         Or is there someway I can access the namespaces declared in the
> root element?
for sure !
see </FONT>
<A HREF="http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getNamespace("><U>http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getNamespace(</U></A><FONT COLOR="#737373">)
and </FONT><A HREF="http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getAdditionalNamespaces("><U>http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getAdditionalNamespaces(</U></A><FONT COLOR="#737373">)
>         In other words, what does it get me that namespaces are declared
> in the root element?
here is an sample that might give you some clue
Retrieve all the namespaces defined on the root element
and choose which object (Namespace objet) defines your prefix
or you URI... Then use this namespace objet as getElement or
getAttribute paramter...
It's written in jython (to be shorter and faster)
------------------------------------------------------------
from java.io import *
from java.lang import *
from org.jdom.input import *
from org.jdom.xpath import *
xmlbuffer ="""<?xml version='1.0' encoding='ISO-8859-1'?>
<rootElt xmlns:mets="</FONT><A HREF="http://www.loc.gov/METS/"><U>http://www.loc.gov/METS/"</U></A>
<FONT COLOR="#737373">        xmlns:moa2="</FONT><A HREF="http://sunsite.berkeley.edu/MOA2/"><U>http://sunsite.berkeley.edu/MOA2/"</U></A>
<FONT COLOR="#737373">        xmlns:mods="</FONT><A HREF="http://www.loc.gov/mods/v3"><U>http://www.loc.gov/mods/v3"</U></A>
<FONT COLOR="#737373">        xmlns:mix="</FONT><A HREF="http://www.loc.gov/mix/"><U>http://www.loc.gov/mix/"</U></A>
<FONT COLOR="#737373">        xmlns:xlink="</FONT><A HREF="http://www.w3.org/TR/xlink"><U>http://www.w3.org/TR/xlink"</U></A>
<FONT COLOR="#737373">        xmlns:xsi="</FONT><A HREF="http://www.w3.org/2001/XMLSchema-instance"><U>http://www.w3.org/2001/XMLSchema-instance"</U></A>
<FONT COLOR="#737373">>
<mix:a mods:attr="test"/>
</rootElt>
"""
docBuilder = SAXBuilder()
doc = SAXBuilder().build(StringReader(xmlbuffer))
Result = StringBuffer()
nsList = doc.getRootElement().getAdditionalNamespaces()
mixNS = modsNS = None
for ns in nsList:
        if ns.getURI() == "</FONT><A HREF="http://www.loc.gov/mix/"><U>http://www.loc.gov/mix/":</U></A>
<FONT COLOR="#737373">                mixNS = ns
        elif ns.getURI() == "</FONT><A HREF="http://www.loc.gov/mods/v3"><U>http://www.loc.gov/mods/v3":</U></A>
<FONT COLOR="#737373">                modsNS = ns
EltA = doc.getRootElement().getChild("a",mixNS)
print "Elt A : ",EltA
attr = EltA.getAttribute("attr",modsNS)
print "Attr name (",attr.getName(),") value (",attr.getValue(),")"
print "Attr dump : ",attr
------------------------------------------------------------
output is
[java] Elt A : [Element: <mix:a [Namespace: </FONT><A HREF="http://www.loc.gov/mix/]/"><U>http://www.loc.gov/mix/]/</U></A><FONT COLOR="#737373">>]
[java] Attr name ( attr ) value ( test )
[java] Attr dump : [Attribute: mods:attr="test"]
HTH
cheers</I></FONT></PRE>
</BLOCKQUOTE>
<PRE><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
-- <BR>
Phil Weighill-Smith <<A HREF="mailto:phil.weighill-smith@volantis.com"><U>phil.weighill-smith@volantis.com</U></A>><BR>
Volantis Systems
</TD>
</TR>
</TABLE>
</PRE>
</BODY>
</HTML>