I need to supply a document in a form that uses scoped default namespaces, and I can't figure out how to get XMLBeans to create it. The consumer of the document defines what it looks like, and is quite determined that what they want works just fine with other partners. (My suspicion is that they're using scripting languages and creating XML shaped text, rather than XML aware tools... But never mind.)
I'm using java 5, xmlbeans 1.0.3. Upgrading XML Beans is possible, but a big hassle with potential side effects I'd prefer to avoid if I can. The kind of thing I'm trying to do can be seen at http://www.datypic.com/books/defxmlschema/chapter20.html, example 20-13. Note the default namespace definitions in the customer name and items product elements. The schema for that document are a little up the page, example 20-11 (although the prod schema is missing a ProdNumType definition, fixed in the version included below.) How do I use XMLBeans to replicate example 20-13? I'm going to put a lot of files inline in this message, having had a lot of trouble with code attachments and some spam filters. If you don't want to look at code, stop here. Included are the schema files I'm using, the ant target that compiles them, and the junit test program that exercises the options I'm trying. I'll include the output, too. ===== ord.xsd ===== <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://example.org/prod" xmlns:cust="http://example.org/cust" xmlns="http://example.org/ord" targetNamespace="http://example.org/ord" elementFormDefault="qualified"> <xsd:import schemaLocation="prod.xsd" namespace="http://example.org/prod"/> <xsd:import schemaLocation="cust.xsd" namespace="http://example.org/cust"/> <xsd:element name="order" type="OrderType"/> <xsd:complexType name="OrderType"> <xsd:sequence> <xsd:element name="customer" type="cust:CustomerType"/> <xsd:element name="items" type="prod:ItemsType"/> </xsd:sequence> </xsd:complexType> </xsd:schema> ===== prod.xsd ===== <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/prod" targetNamespace="http://example.org/prod" elementFormDefault="qualified"> <xsd:complexType name="ItemsType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="product" type="ProductType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="ProdNumType" > <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> ===== cust.xsd ===== <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/cust" targetNamespace="http://example.org/cust" elementFormDefault="qualified"> <xsd:complexType name="CustomerType"> <xsd:sequence> <xsd:element name="name" type="CustNameType"/> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="CustNameType"> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:schema> ===== ant target that generates jar files ===== <target name="gen-xmlbeans" depends="init" description="generate XMLBean code from schema" > <xmlbean schema="ord.xsd" srcgendir="${src.dir}" destfile="${dist.dir}/ord.jar" classpathref="compile.classpath" fork="no" debug="true" verbose="true"/> <xmlbean schema="cust.xsd" srcgendir="${src.dir}" destfile="${dist.dir}/cust.jar" classpathref="compile.classpath" fork="no" debug="true" verbose="true"/> <xmlbean schema="prod.xsd" srcgendir="${src.dir}" destfile="${dist.dir}/prod.jar" classpathref="compile.classpath" fork="no" debug="true" verbose="true"/> </target> ===== NamespaceTest.java ===== package com.test; import org.apache.xmlbeans.XmlOptions; import org.example.cust.CustomerType; import org.example.ord.OrderDocument; import org.example.ord.OrderType; import org.example.prod.ItemsType; import org.example.prod.ProductType; import junit.framework.TestCase; public class NamespaceTest extends TestCase { private OrderDocument orderDoc; private XmlOptions printOpts; protected void setUp() throws Exception { printOpts = new XmlOptions(); printOpts.setSavePrettyPrint(); printOpts.setSavePrettyPrintIndent(2); } private void populateOrder() { OrderType order = orderDoc.addNewOrder(); CustomerType cust = order.addNewCustomer(); cust.setName("Priscilla Walmsley"); ItemsType items = order.addNewItems(); ProductType prod = items.addNewProduct(); prod.setNumber("557"); } protected void tearDown() throws Exception { } public void testNoPrintOpts() throws Exception { System.out.println("testNoPrintOpts"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText()); } public void testPrettyPrintOpts() throws Exception { System.out.println("\ntestPrettyPrintOpts"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText(printOpts)); } public void testPPAndAggressiveNamespaces() throws Exception { printOpts.setSaveAggresiveNamespaces(); System.out.println("\ntestPPAndAggressiveNamespaces"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText(printOpts)); } public void testPPEtcWithDefaultNS() throws Exception { printOpts.setUseDefaultNamespace(); printOpts.setSaveAggresiveNamespaces(); System.out.println("\ntestPPEtcWithDefaultNS"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText(printOpts)); } public void testPPWithDefaultNSNotAggressive() throws Exception { printOpts.setUseDefaultNamespace(); System.out.println("\ntestPPEtcWithDefaultNSNotAggressive"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText(printOpts)); } public void testPPWithDefaultNSNotAggressiveButFirst() throws Exception { printOpts.setUseDefaultNamespace(); printOpts.setSaveNamespacesFirst(); System.out.println("\ntestPPEtcWithDefaultNSNotAggressiveButFirst"); orderDoc = OrderDocument.Factory.newInstance(); populateOrder(); System.out.println(orderDoc.xmlText(printOpts)); } } ===== sample output from test program ===== testNoPrintOpts <ord:order xmlns:ord="http://example.org/ord"><ord:customer><cust:name xmlns:cust="http://example.org/cust">Priscilla Walmsley</cust:name></ord:customer><ord:items><prod:product xmlns:prod="http://example.org/prod"><prod:number>557</prod:number></prod:pr oduct></ord:items></ord:order> testPrettyPrintOpts <ord:order xmlns:ord="http://example.org/ord"> <ord:customer> <cust:name xmlns:cust="http://example.org/cust">Priscilla Walmsley</cust:name> </ord:customer> <ord:items> <prod:product xmlns:prod="http://example.org/prod"> <prod:number>557</prod:number> </prod:product> </ord:items> </ord:order> testPPAndAggressiveNamespaces <ord:order xmlns:ord="http://example.org/ord" xmlns:cust="http://example.org/cust" xmlns:prod="http://example.org/prod"> <ord:customer> <cust:name>Priscilla Walmsley</cust:name> </ord:customer> <ord:items> <prod:product> <prod:number>557</prod:number> </prod:product> </ord:items> </ord:order> testPPEtcWithDefaultNS <order xmlns="http://example.org/ord" xmlns:cust="http://example.org/cust" xmlns:prod="http://example.org/prod"> <customer> <cust:name>Priscilla Walmsley</cust:name> </customer> <items> <prod:product> <prod:number>557</prod:number> </prod:product> </items> </order> testPPEtcWithDefaultNSNotAggressive <order xmlns="http://example.org/ord"> <customer> <cust:name xmlns:cust="http://example.org/cust">Priscilla Walmsley</cust:name> </customer> <items> <prod:product xmlns:prod="http://example.org/prod"> <prod:number>557</prod:number> </prod:product> </items> </order> testPPEtcWithDefaultNSNotAggressiveButFirst <order xmlns="http://example.org/ord"> <customer> <cust:name xmlns:cust="http://example.org/cust">Priscilla Walmsley</cust:name> </customer> <items> <prod:product xmlns:prod="http://example.org/prod"> <prod:number>557</prod:number> </prod:product> </items> </order> -- Dennis R. Sherman Ex Libris Group 847-227-2976 [EMAIL PROTECTED] http://www.exlibrisgroup.com --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

