[jdom-interest] How do I use namespaces?
Phil Weighill-Smith
phil.weighill-smith at volantis.com
Thu Feb 5 02:02:13 PST 2004
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 not 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).
You can do something like:
private static final Properties URIS = new Properties();
static {
URIS.load(getClass().getResourceAsStream("uris.properties"));
}
private static final String MODS_URI = URIS.getProperty("mods");
private static final String MIX_URI = URIS.getProperty("mix");
private static final Namespace MODS =
Namespace.getNamespace("totallyArbitraryPrefixDoesNotHaveToMatchTheDocumentButIsUniqueInThisDocumentsSetOfPrefixes", MODS_URI);
private static final Namespace MIX =
Namespace.getNamespace("anotherTotallyArbitraryPrefixDoesNotHaveToMatchTheDocumentButIsUniqueInThisDocumentsSetOfPrefixes", MIX_URI);
...
and to access the required attribute in the MODS namespace from the
required element in the MIX namespace:
doc.getRootElement().getChild("a", MIX).getAttribute("x", MODS);
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.
Hope that's helpful!
Phil :n)
PS: You would put the file "uris.properties" in the class's package and
populate it like:
# Define the URIs used in the code. The key is not the prefix
# used in the XML document or document accessing code
# but is just a meaningful key for accessing the URI definitions
mods=http://www.loc.gov/mods/v3
mix=http://www.loc.gov/mix/
On Thu, 2004-02-05 at 08:58, Frédéric Laurent wrote:
> Quoting Garey Mills <gmills at 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="http://www.loc.gov/METS/"
> > xmlns:moa2="http://sunsite.berkeley.edu/MOA2/"
> > xmlns:mods="http://www.loc.gov/mods/v3"
> > xmlns:mix="http://www.loc.gov/mix/"
> > xmlns:xlink="http://www.w3.org/TR/xlink"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >
> >
> > 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
> http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getNamespace()
> and http://www.jdom.org/docs/apidocs/org/jdom/Element.html#getAdditionalNamespaces()
>
> > 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="http://www.loc.gov/METS/"
> xmlns:moa2="http://sunsite.berkeley.edu/MOA2/"
> xmlns:mods="http://www.loc.gov/mods/v3"
> xmlns:mix="http://www.loc.gov/mix/"
> xmlns:xlink="http://www.w3.org/TR/xlink"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >
> <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() == "http://www.loc.gov/mix/":
> mixNS = ns
> elif ns.getURI() == "http://www.loc.gov/mods/v3":
> 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: http://www.loc.gov/mix/]/>]
> [java] Attr name ( attr ) value ( test )
> [java] Attr dump : [Attribute: mods:attr="test"]
>
>
> HTH
> cheers
--
Phil Weighill-Smith <phil.weighill-smith at volantis.com>
Volantis Systems
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040205/1416b625/attachment.htm
More information about the jdom-interest
mailing list