users@jaxb.java.net

recursively nested elements

From: Hyun Shin <hshin_at_fsc-usa.com>
Date: Mon, 17 Mar 2003 14:37:26 -0700

Hi,

I have following schema.
Where Element A, B are identical except their order.
In this case, the resulting test code (Main.java) below failed on every operation on the element A, while the operstions on the element b are all ok. If I use anonymous complex type then it works ok for both.

BTW, I am using JAXB1.0 RI.

Regards,
-Hyun

Schema test.xsd (compile with "xjc.sh test.xsd -p test") ---

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

  <xs:element name="A" type="AT" />
  <xs:element name="B" type="BT" />

  <xs:complexType name="AT" >
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="A" />
        <xs:element ref="B" />
      </xs:choice>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="title" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="BT" >
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="A" />
        <xs:element ref="B" />
      </xs:choice>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="title" type="xs:string" />
  </xs:complexType>

</xs:schema>

Main.java ---

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import javax.xml.bind.*;
import test.*;

public class Main {

    public static void main( String[] args ) {
                String file = "cdl.xml";
                if (args.length > 0)
                        file = args[0];

        System.out.println("Testing Element A ");
        try {
            JAXBContext jc = JAXBContext.newInstance( "test" );

            A s = new test.ObjectFactory().createA();
                        s.setName("NewSub");
                        s.setTitle("NewSubTitle");
                        Validator v = jc.createValidator();
                        boolean valid = v.validate( s );
                        System.out.println("Validating Element A " + valid );

                        Marshaller m = jc.createMarshaller();
                        m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
                        m.marshal(s, System.out);
        } catch( JAXBException je ) {
                        System.out.println("Testing Element A failed : " + je.getMessage());
        }

        System.out.println("Testing Element B ");
        try {
            JAXBContext jc = JAXBContext.newInstance( "test" );

            B s = new test.ObjectFactory().createB();
                        s.setName("NewSub");
                        s.setTitle("NewSubTitle");
                        Validator v = jc.createValidator();
                        boolean valid = v.validate( s );
                        System.out.println("Validating Element B " + valid );

                        Marshaller m = jc.createMarshaller();
                        m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
                        m.marshal(s, System.out);
        } catch( JAXBException je ) {
                        System.out.println("Testing Element B failed : " + je.getMessage());
        }



    }
}

Here is output of Main.java

Testing Element A
DefaultValidationEventHandler: [ERROR]: unexpected attribute "title"
Testing Element A failed : null
Testing Element B
Validating Element B true
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<B title="NewSubTitle" name="NewSub"></B>