title: JAXB/XJC: handling multiple namespaces (element doesn't contain any child node)
body: <p>I'm trying to create a XSD schema to handle the following ontology with XJC/JAXB :</p>
  
  <p><a href="http://www.geneontology.org/GO.downloads.ontology.shtml#ont" rel="nofollow">http://archive.geneontology.org/latest-termdb/go_daily-termdb.rdf-xml.gz</a> </p>
  
  <p>(I know is there no schema for RDF  but this file is always formatted the same way). The file looks like:</p>
  
  <pre><code>&lt;go:go xmlns:go="http://www.geneontology.org/dtds/go.dtd#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"&gt;
      &lt;rdf:RDF&gt;
          &lt;go:term rdf:about="http://www.geneontology.org/go#GO:0000001"&gt;
              &lt;go:accession&gt;GO:0000001&lt;/go:accession&gt;
              &lt;go:name&gt;mitochondrion inheritance&lt;/go:name&gt;
              &lt;go:synonym&gt;mitochondrial inheritance&lt;/go:synonym&gt;
              &lt;go:definition&gt;The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by
   interactions between mitochondria and the cytoskeleton.&lt;/go:definition&gt;
              &lt;go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048308" /&gt;
              &lt;go:is_a rdf:resource="http://www.geneontology.org/go#GO:0048311" /&gt;
          &lt;/go:term&gt;
          &lt;go:term rdf:about="http://www.geneontology.org/go#GO:0000002"&gt;
         (...)
  </code></pre>
  
  <p>mixing two namespaces 'go:' and 'rdf:'.</p>
  
  <p>I've tried to create a XSD schema. Here are my xsd so far:</p>
  
  <h3>go.xsd</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:go="http://www.geneontology.org/dtds/go.dtd#"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      elementFormDefault="qualified"
      targetNamespace="http://www.geneontology.org/dtds/go.dtd#"
      attributeFormDefault="qualified"
      &gt;
  
  &lt;xs:import namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#" schemaLocation="rdf.xsd"/&gt;
  
  &lt;xs:group name="is_group"&gt;
    &lt;xs:choice&gt;
        &lt;xs:element name="is_a" type="go:IsA" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="part_of" type="go:PartOf" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="negatively_regulates" type="go:NegativelyRegulates" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="positively_regulates" type="go:PositivelyRegulates" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="regulates" type="go:Regulates" minOccurs="0" maxOccurs="unbounded"/&gt;
    &lt;/xs:choice&gt;
  &lt;/xs:group&gt;
  
  
    &lt;xs:complexType name="Term"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="accession" type="xs:string" minOccurs="1" maxOccurs="1"/&gt;
        &lt;xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1"/&gt;
        &lt;xs:element name="synonym" type="xs:string" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="definition" type="xs:string" minOccurs="0" maxOccurs="1"/&gt;
        &lt;xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:group ref="go:is_group" minOccurs="0" maxOccurs="unbounded"/&gt;
        &lt;xs:element name="dbxref" type="go:DbXRef" minOccurs="0" maxOccurs="unbounded"/&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attributeGroup ref="rdf:rdfabout"/&gt;
    &lt;/xs:complexType&gt;
  
  
    &lt;xs:group name="goterm"&gt;
      &lt;xs:sequence&gt;
         &lt;xs:element name="term" type="go:Term"  minOccurs="0" maxOccurs="unbounded" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:group&gt;
  
  
    &lt;xs:complexType name="AbstractRelation" abstract="true"&gt;
      &lt;xs:attributeGroup ref="rdf:rdfrsrc"/&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="IsA"&gt;
          &lt;xs:complexContent&gt;
          &lt;xs:extension base="go:AbstractRelation"&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="PartOf"&gt;
          &lt;xs:complexContent&gt;
          &lt;xs:extension base="go:AbstractRelation"&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="NegativelyRegulates"&gt;
          &lt;xs:complexContent&gt;
          &lt;xs:extension base="go:AbstractRelation"&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="PositivelyRegulates"&gt;
          &lt;xs:complexContent&gt;
          &lt;xs:extension base="go:AbstractRelation"&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="Regulates"&gt;
          &lt;xs:complexContent&gt;
          &lt;xs:extension base="go:AbstractRelation"&gt;
          &lt;/xs:extension&gt;
      &lt;/xs:complexContent&gt;
    &lt;/xs:complexType&gt;
  
  
  
    &lt;xs:complexType name="DbXRef"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="database_symbol" type="xs:string" minOccurs="1" maxOccurs="1"/&gt;
        &lt;xs:element name="reference" type="xs:string" minOccurs="1" maxOccurs="1"/&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attributeGroup ref="rdf:rdfparsetype"/&gt;
    &lt;/xs:complexType&gt;
  
  
    &lt;xs:complexType name="Go"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:group ref="rdf:rdfroot" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  
  &lt;xs:element name="go" type="go:Go" /&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <h3>rdf.xsd</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:go="http://www.geneontology.org/dtds/go.dtd#"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      targetNamespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      elementFormDefault="qualified"
       attributeFormDefault="qualified"
      &gt;
    &lt;xs:import namespace="http://www.geneontology.org/dtds/go.dtd#" schemaLocation="go.xsd"/&gt;
    &lt;xs:attributeGroup name="rdfabout"&gt;
      &lt;xs:attribute name="about" type="xs:string"/&gt;
    &lt;/xs:attributeGroup&gt;
  
    &lt;xs:attributeGroup name="rdfrsrc"&gt;
      &lt;xs:attribute name="resource" type="xs:string"/&gt;
    &lt;/xs:attributeGroup&gt;
  
    &lt;xs:attributeGroup name="rdfparsetype"&gt;
      &lt;xs:attribute name="parseType" type="xs:string" fixed="Resource" /&gt;
    &lt;/xs:attributeGroup&gt;
  
  
  
    &lt;xs:complexType name="RDF"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:group ref="go:goterm" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  
  
  &lt;xs:group name="rdfroot"&gt;
    &lt;xs:sequence&gt;
        &lt;xs:element name="RDF" type="rdf:RDF" minOccurs="1" maxOccurs="1"/&gt;
    &lt;/xs:sequence&gt;
  &lt;/xs:group&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>The rdf file is validated against this schema with <strong>xmllint</strong>.</p>
  
  <pre><code>$ xmllint  --noout --schema go.xsd go.xml
  go.xml validates
  </code></pre>
  
  <p>But, after compiling the xsd with XJC and running the java program below, I don't get the body of go:term:</p>
  
  <pre><code>import java.io.InputStream;
  import java.io.StringWriter;
  import org.geneontology.dtds.go.*;
  import org.w3._1999._02._22_rdf_syntax_ns.*;
  import javax.xml.namespace.QName;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.Unmarshaller;
  import javax.xml.bind.Marshaller;
  import  javax.xml.transform.stream.StreamSource;
  
  
  public class TestGo
      {
      public static void main(String[] args) throws Exception
          {
      JAXBContext jaxbCtxt=JAXBContext.newInstance(JAXBContext.newInstance("org.geneontology.dtds.go:org.w3._1999._02._22_rdf_syntax_ns");
      Marshaller marshaller = jaxbCtxt.createMarshaller();
      Unmarshaller unmarshaller=jaxbCtxt.createUnmarshaller();
          marshaller.setProperty("jaxb.formatted.output",true);
          Go go=unmarshaller.unmarshal( new StreamSource(System.in), Go.class).getValue();
          marshaller.marshal(new JAXBElement&lt;Go&gt;(
              new QName("http://www.geneontology.org/dtds/go.dtd#", "go"),
              Go.class,
              go
              ), System.out);
          }
      }
  </code></pre>
  
  <p>compile &amp;run:</p>
  
  <pre><code>xjc -d tmp ../schemas/bio/go/go.xsd
  javac -d tmp -sourcepath tmp:. TestGo.java
  java -cp tmp TestGo &lt; go.xml | head -n 20
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>&lt;ns3:go xmlns:ns2="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns3="http://www.geneontology.org/dtds/go.dtd#"&gt;
      &lt;ns2:RDF&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000001"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000002"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000003"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000005"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0042254"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0044183"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0051082"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000006"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000007"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000008"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0003756"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0015036"/&gt;
          &lt;ns3:term ns2:about="http://www.geneontology.org/go#GO:0000009"/&gt;
  </code></pre>
  
  <p>What's wrong ? how can I get the body of 'go:term' ? is it the right way to handle the group (e.g: the xsd:group name="goterm" looks ugly to me )</p>
  
  <p>Edit: changed JAXBContext.newInstance according to Blaise's comment.</p>
  
  <p>Edit: second java code for Blaise.</p>
  
  <pre><code>import java.io.InputStream;
  import java.io.StringWriter;
  import org.geneontology.dtds.go.*;
  import org.w3._1999._02._22_rdf_syntax_ns.*;
  import javax.xml.namespace.QName;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.Unmarshaller;
  import javax.xml.bind.Marshaller;
  import  javax.xml.transform.stream.StreamSource;
  
  
  public class TestGo2
      {
      public static void main(String[] args) throws Exception
          {
          Go go=new Go();
          RDF rdf=new RDF();
          go.setRDF(rdf);
          for(int i=0;i&lt;2;++i)
              {
              Term t=new Term();
              t.setAbout("uri:"+i);
              t.setAccession("Hello"+i);
              rdf.getTerm().add(t);
              }
  
      JAXBContext jaxbCtxt=JAXBContext.newInstance("org.geneontology.dtds.go:org.w3._1999._02._22_rdf_syntax_ns");
      Marshaller marshaller = jaxbCtxt.createMarshaller();
  
          marshaller.marshal(new JAXBElement&lt;Go&gt;(
              new QName("http://www.geneontology.org/dtds/go.dtd#", "go"),
              Go.class,
              go
              ), System.out);
          }
      }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns3:go xmlns:ns2="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns3="http://www.geneontology.org/dtds/go.dtd#"&gt;
  &lt;ns2:RDF&gt;
  &lt;ns3:term ns2:about="uri:0"&gt;
   &lt;accession&gt;Hello0&lt;/accession&gt;
  &lt;/ns3:term&gt;
  &lt;ns3:term ns2:about="uri:1"&gt;
    &lt;accession&gt;Hello1&lt;/accession&gt;
  &lt;/ns3:term&gt;
  &lt;/ns2:RDF&gt;
  &lt;/ns3:go&gt;
  </code></pre>
  
  <p>and here is my current Makefile:</p>
  
  <pre><code>JAVA_HOME:=$(dir $(shell which java))..
  JAVAC=${JAVA_HOME}/bin/javac
  JAVA=${JAVA_HOME}/bin/java
  XJC?=${JAVA_HOME}/bin/xjc
  
  .PHONY=all go clean
  all:go
  
  go: go.xml
      -xmllint  --noout --schema ../schemas/bio/go/go.xsd $&lt;
      mkdir -p tmp
      ${XJC} -d tmp ../schemas/bio/go/go.xsd 
      ${JAVAC} -d tmp -sourcepath tmp:. TestGo.java `find tmp -name "ObjectFactory.java"`
      ${JAVA}  -cp tmp TestGo &lt; $&lt; | head -n 20
      #rm -rf tmp
  
  go.xml:
      curl "http://archive.geneontology.org/latest-termdb/go_daily-termdb.rdf-xml.gz" |\
      gunzip -c | grep -v "&lt;!DOCTYPE " &gt; $@
  
  clean:
      rm -rf tmp go.xml
  </code></pre>