Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] namespace question

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Kevin S. Clarke" <ksclarke AT stanford.edu>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] namespace question
  • Date: Fri, 09 May 2003 00:43:15 -0000

Hi,

I am getting a blank namespace when I serialize a document. I want a
default namespace that is not repeated on each element so I set it on
root. I also add another namespace declaration with prefix that I can
use on deep children (with the URI also declared on the root element).

// Currently, Serializer.java says:

String uri = element.getNamespaceURI(additionalPrefix);
if (parentNode instanceof Element) {
Element parentElement = (Element) parentNode;
if (uri.equals(parentElement.getNamespaceURI(additionalPrefix))) {
// I added brackets b/c this wrapped in my email client
continue;
}
}
else if (uri.equals("")) {
continue; // no need to say xmlns=""
}

// If it said instead:

String uri = element.getNamespaceURI(additionalPrefix);
if (uri.equals("")) {
continue; // no need to say xmlns=""
}
else if (parentNode instanceof Element) {
Element parentElement = (Element) parentNode;
if (uri.equals(parentElement.getNamespaceURI(additionalPrefix))) {
// I added brackets b/c this wrapped in my email client
continue;
}
}

// end

there would be no problem (but I haven't thought this through so it may
introduce other problems farther down)... If I'm missing something big
just let me know...

Thanks,
Kevin


--sample code--
import nu.xom.Serializer;
import nu.xom.Document;
import nu.xom.Element;

import java.io.IOException;

public class Test {
public static void main(String[] args) throws IOException {
Serializer writer = new Serializer(System.out);
Element root = new Element("root");
Document document = new Document(root);
String fakeNS = "http://fake.com/namespace";;

root.addNamespaceDeclaration("fake", fakeNS);
root.setNamespaceURI("http://default.com/namespace/";);

Element e1 = new Element("e1");
Element e2 = new Element("e2");
Element e3 = new Element("e3");
Element e4 = new Element("fake:e4", fakeNS);

e2.appendChild(e3);
e2.appendChild(e4);
e1.appendChild(e2);
root.appendChild(e1);

writer.setIndent(4);
writer.write(document);
}
}

--sample result--
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.loc.gov/mods/";
xmlns:fake="http://fake.com/namespace";>
<e1 xmlns="">
<e2>
<e3/>
<fake:e4/>
</e2>
</e1>
</root>


--
There are two kinds of people in this world: those that believe there
are two kinds of people and those that know better.





Archive powered by MHonArc 2.6.24.

Top of Page