Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Builder argument swap

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Bradley S. Huffman" <hip AT cs.okstate.edu>
  • To: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Builder argument swap
  • Date: Mon, 22 Mar 2004 20:16:21 -0600

Elliotte Rusty Harold writes:

> We've discussed this in the past, and the result was the current
> behavior. However, I could be convinced that elements should retain
> their base URI on being detached from the parent. Right now that
> isn't obviously right though.

Right now sometimes they retain there base URI when they're detached,
sometimes they don't, it all depends on how the base URI is set.

> >Add the removed element to another document with a base attribute somewhere
> >up the parent chain and getBaseURI will return non-null.
>
> More specifically it will return the base URI in scope in that new
> document. I think this is what most users will expect; ie.e. it
> follows the principle of least surprise; but I'm not absolutely
> convinced here.

Again depends on how the base URI was set.

For example if test.xml is

<root><foo><bah/></foo></root>

and I run this code

import java.io.IOException;
import nu.xom.*;

public class TestBaseURI {

public static void main(String[] args) {

//
// Build document from file
//
Builder builder0 = new Builder();
try {
Document doc0 = builder0.build("test.xml");

Element root0 = doc0.getRootElement();

System.out.println("\nDocument 0");
System.out.println("----------");
printBaseURI(root0);

Element e0 = root0.getFirstChildElement("foo");
root0.removeChild(e0);

System.out.println("\nElement foo Removed");
System.out.println("---------------------");
printBaseURI(e0);

Element n0 = new Element("bar");
n0.setBaseURI("http://www.acme.org";);
n0.appendChild(e0);

System.out.println("\nElement foo Added to another element");
System.out.println("------------------------------------");
printBaseURI(n0);
}
catch (IOException ex) {
System.err.println(ex);
}
catch (ParsingException ex) {
System.err.println(ex);
}

//
// Build document by hand
//
Element root1 = new Element("root");
root1.setBaseURI("file://home/hip/text-xom/test.xml");
Document doc1 = new Document(root1);

Element foo = new Element("foo");
foo.setBaseURI("file://home/hip/text-xom/test.xml");
Element bah = new Element("bah");
bah.setBaseURI("file://home/hip/text-xom/test.xml");

root1.appendChild(foo);
foo.appendChild(bah);

System.out.println("\nDocument 1");
System.out.println("----------");
printBaseURI(root1);

Element e1 = root1.getFirstChildElement("foo");
root1.removeChild(e1);

System.out.println("\nElement foo Removed");
System.out.println("---------------------");
printBaseURI(e1);

Element n1 = new Element("bar");
n1.setBaseURI("http://www.acme.org";);
n1.appendChild(e1);

System.out.println("\nElement foo Added to another element");
System.out.println("------------------------------------");
printBaseURI(n1);
}

public static void printBaseURI(Element el) {
String qname = el.getQualifiedName();
String base = el.getBaseURI();

System.out.println(qname + " base uri=" + base);

Elements elements = el.getChildElements();
for (int i = 0; i < elements.size(); i++) {
printBaseURI(elements.get(i));
}
}
}

I get the following results

Document 0
----------
root base uri=file:///home/hip/test-xom/test.xml
foo base uri=file:///home/hip/test-xom/test.xml
bah base uri=file:///home/hip/test-xom/test.xml

Element foo Removed
---------------------
foo base uri=null
bah base uri=null

Element foo Added to another element
------------------------------------
bar base uri=http://www.acme.org
foo base uri=http://www.acme.org
bah base uri=http://www.acme.org

Document 1
----------
root base uri=file://home/hip/text-xom/test.xml
foo base uri=file://home/hip/text-xom/test.xml
bah base uri=file://home/hip/text-xom/test.xml

Element foo Removed
---------------------
foo base uri=file://home/hip/text-xom/test.xml
bah base uri=file://home/hip/text-xom/test.xml

Element foo Added to another element
------------------------------------
bar base uri=http://www.acme.org
foo base uri=file://home/hip/text-xom/test.xml
bah base uri=file://home/hip/text-xom/test.xml

Which just hits me a violation of the principle of least surprise. I just
cann't help but think if using StringReader is inobvious to a good many
users then this will really confuse them.

However, I not sure what should be done. Should Builder call setBaseURI for
every element (right now it only calls setBaseURI on the document node)?

Brad




Archive powered by MHonArc 2.6.24.

Top of Page