[jboss-dev-forums] [Design of JBossXB] - Re: JBossXB does not assign schema defined attribute default
alex.loubyansky@jboss.com
do-not-reply at jboss.com
Thu Jul 2 18:07:27 EDT 2009
I've committed the testcase here
http://anonsvn.jboss.org/repos/common/jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java
and the xml and xsd are here
http://anonsvn.jboss.org/repos/common/jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/
As mentioned above, the problem is the XML parser cannot resolve the XSD.
addSchemaLocation(XMLNS_BLUEPRINT, "blueprint.xsd"); won't help. This will configure XsdBinder to resolve schema to class mapping (i.e. this is binding layer) but it has nothing to do with the XML parser.
So, here is the testcase I committed
UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
| Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
|
| // this is to make the SAX parser parse the XSD as well and include default attribute values in the startElement
| unmarshaller.setSchemaValidation(true);
|
| // this is EntityResolver that SAX parser (xerces) will use to resolve XSD location for the XML being parsed
| JBossEntityResolver xmlResolver = new JBossEntityResolver();
| // Here we map schema location specified in the XML file to the local schema location,
| // which is a path relative to the resources directory visible in the classpath.
| // Note, we have to specify here and XML the complete URL including the protocol part into the schemaLocation.
| // The reason is that if we specify schemaLocation in XML as just a filename,
| // the SAX parser will compose its schemaLocation by adding the protocol, baseURI (which will depend on the
| // environment the code is run in) and the schemaLocation we specified in the XML and then
| // will pass this new schemaLocation to the resolver.
| xmlResolver.registerLocalEntity("http://www.hostame.org/SchemaDefaultAttributeValue.xsd", "org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd");
| unmarshaller.setEntityResolver(xmlResolver);
|
| // this is to resolve namespace to class mapping and build the SchemaBinding from the class
| MultiClassSchemaResolver schemaBindingResolver = new MultiClassSchemaResolver();
| schemaBindingResolver.mapURIToClass("xb:test:default-attribute", DefaultAttribute.class);
| // the reason we configured a separate EntityResolver for unmarshaller above instead of
| // calling schemaBindingResolver.mapSchemaLocation(nsURI, location) is that
| // entity resolution inside schemaBindingResolver is used only for nsURI to class resolution (by XsdBinder)
| // and is not related to SAX parser entity resolution and XML validation.
|
| String xml = findXML("SchemaDefaultAttributeValue.xml");
| Object result = unmarshaller.unmarshal(xml, schemaBindingResolver);
XSD
<xsd:schema
| targetNamespace="xb:test:default-attribute"
| xmlns:xsd="http://www.w3.org/2001/XMLSchema"
| elementFormDefault="qualified"
| attributeFormDefault="unqualified">
|
| <xsd:element name="default-attribute">
| <xsd:complexType>
| <xsd:attribute name="attribute" type="xsd:int" default="123" />
| </xsd:complexType>
| </xsd:element>
| </xsd:schema>
XML
<default-attribute xmlns="xb:test:default-attribute"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xsi:schemaLocation="xb:test:default-attribute http://www.hostame.org/SchemaDefaultAttributeValue.xsd"/>
I agree, XSD resolution and its configuration is confusing. That's something we could work on and make simpler.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241616#4241616
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241616
More information about the jboss-dev-forums
mailing list