users@jaxb.java.net

Re: restriction of a type containing a "ref" element

From: Ed Mooney <Ed.Mooney_at_sun.com>
Date: Tue, 03 Jun 2003 13:15:37 -0400

Hi Fran?ois,

I was unable to unmarshal your xml file without error using Test.java:

     java Test
     LimitType = String

All I can think of is that perhaps you're loading objects generated from
any earlier version of your schema?

Regards,
--
Ed Mooney         |Sun Microsystems, Inc.|Time flies like
Java Web Services |UBUR02-201            |an arrow, but
Ed.Mooney_at_Sun.COM |1 Network Drive       |fruit flies like
781-442-0459      |Burlington, MA  01803 |a banana. Groucho
Fran?ois Banel wrote:
> Hello,
>
> I think I have found a bug in the JAXB 1.0 version.
> I have not found any reference to that problem in previous messages,
> so I post it :
>
> in the same namespace, I define an element TimeLimit, and types OrderType
> and SimpleOrderType.
> OrderType contains a ref to a TimeLimit element.
> SimpleOrderType is a restriction of OrderType, containing also the TimeLimit element.
>
> Then by unmarshalling a correct XML file (correct for XMLSpy, but it appears to be simple enough to be correct anyway), I obtain an object
> which cannot be validated by JAXB.
>
> The Validation Handler message is :
> message= "tag name "TimeLimit" is not allowed. Possible tag names are: <com.testfb.datamodel.restriction.TimeLimit>"
>
> Am I wrong in defining the schema or is it an error from JAXB ?
>
>
> The sample schema file :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <schema targetNamespace="http://www.datamodel.testfb.com/restriction" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:res="http://www.datamodel.testfb.com/restriction" jxb:version="1.0">
>
>  <element name="TimeLimit">
>   <complexType>
>    <sequence>
>     <element name="ExpiryDate" type="date" minOccurs="0"/>
>    </sequence>
>    <attribute name="LimitType" type="string"/>
>   </complexType>
>  </element>
>
>  <complexType name="OrderType">
>   <sequence>
>    <element ref="res:TimeLimit" minOccurs="0"/>
>   </sequence>
>  </complexType>
>
>
>  <complexType name="SimpleOrderType">
>   <complexContent>
>    <restriction base="res:OrderType">
>     <sequence>
>      <element ref="res:TimeLimit" minOccurs="0"/>
>     </sequence>
>    </restriction>
>   </complexContent>
>  </complexType>
>  <element name="Order" type="res:SimpleOrderType"/>
> </schema>
>
>
> The sample xml file :
> <?xml version="1.0" encoding="UTF-8"?>
> <res:Order xmlns:res="http://www.datamodel.testfb.com/restriction" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.datamodel.testfb.com/restriction
> C:\order_restriction.xsd">
>  <res:TimeLimit LimitType="String">
>   <ExpiryDate>1967-08-13</ExpiryDate>
>  </res:TimeLimit>
> </res:Order>


/*
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.testfb.datamodel.restriction.Order;
import com.testfb.datamodel.restriction.TimeLimitType;

public class Test {

    public static void main( String[] args ) {

        try {
            JAXBContext jc = JAXBContext.newInstance( "com.testfb.datamodel.restriction" );

            Unmarshaller u = jc.createUnmarshaller();

            u.setValidating(true);

            Order order =
                (Order)u.unmarshal( new FileInputStream( "res.xml" ) );

            TimeLimitType limit = order.getTimeLimit();

            System.out.println("LimitType = " + limit.getLimitType());

        } catch( JAXBException je ) {
            je.printStackTrace();
        } catch( IOException ioe ) {
            ioe.printStackTrace();
        }
    }

}