Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10855

xml:id

From Michael Jung <miju@golem.phantasia.org>
Newsgroups comp.lang.java.programmer
Subject xml:id
Date 2011-12-18 21:33 +0100
Message-ID <87wr9t1uxp.fsf@golem.phantasia.org> (permalink)

Show all headers | View raw


I have a problem with transfering xml:ids from one document to another,
sample code is attached. Somehow the id attribute gets lost. Am I doing
something wrong, missing something, or is this a bug in the XML libs (I
use the ones supplied with the standard JDK)? Maybe this is a "feature"?
It is rather annoying if this doesn't work, since it forces me to to
travers the tree and do id handling myself.

The code produces the same output under OpenJDK, Sun's JDK 1.6 and 1.5:
: [elem: null]
: [elem: null]
: null

=== SimleTest.java ===
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class SimpleTest {
    public static void main(String[] a) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document parsed = docBuilder.parse(new File("test.xml"));
        System.out.println(parsed.getElementById("x"));
        Document parsed2 = docBuilder.parse(new File("test.xml"));
        Element el = parsed.getElementById("x");
        el.setAttribute("id", "x2");
        System.out.println(parsed.getElementById("x2"));
        // I have tried importNode as well, that even loses the "isId"
        // property of the "id" tag.
        parsed2.adoptNode(el);
        // I definitely want to avoid the next call, since I'd need to
        // traverse in production code. But it is useless anyway.
        el.setIdAttribute("id", true);
        System.out.println(parsed2.getElementById("x2"));
    }
}
=== test.xml ===
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./test.xsd">
   <elem id="x"/>
</test>
=== test.xsd ===
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="test">
      <xs:complexType>
         <xs:choice>
            <xs:element name="elem">
               <xs:complexType>
                  <xs:attribute name="id" type="xs:ID" />
               </xs:complexType>
            </xs:element>
         </xs:choice>
      </xs:complexType>
   </xs:element>
</xs:schema>

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-18 21:33 +0100
  Re: xml:id Lee Fesperman <firstsql@gmail.com> - 2011-12-19 00:24 -0800
    Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-19 10:27 +0100
      Re: xml:id Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-12-19 11:25 -0400
        Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-19 19:06 +0100
          Re: xml:id Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-12-19 17:26 -0400
            Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-20 22:54 +0100

csiph-web