You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-user@xml.apache.org by Stefan Armbruster <st...@armbruster-it.de> on 2004/07/23 12:06:45 UTC
anyType and namespaces
Hi,
I've got some problems with namespace declarations when using
"xs:anyType". In order to make the situation as clear as possible, I got
a stripped down example:
----- schema ------
<xs:schema targetNamespace="http://mysample.org/sample1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="name" minOccurs="1" maxOccurs="1"
type="xs:string"/>
<xs:element name="property" minOccurs="1" maxOccurs="1"
type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="color">
<xs:restriction base="xs:string">
<xs:enumeration value="red"/>
<xs:enumeration value="green"/>
<xs:enumeration value="blue"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
----- END: schema -----
Among others, the color type might be used for the element property.
Using the following java code, a XML document is printed,
----- java --------
ProductDocument pd = ProductDocument.Factory.newInstance();
ProductDocument.Product p =
ProductDocument.Product.Factory.newInstance();
p.setName("Product 1");
Color prop = Color.Factory.newInstance();
prop.setStringValue("red");
p.setProperty(prop);
pd.setProduct(p);
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
options.setUseDefaultNamespace();
options.setSaveAggresiveNamespaces();
pd.save(System.out, options);
------ END: java----
---- console-output ----
<?xml version="1.0" encoding="UTF-8"?>
<product xmlns="http://mysample.org/sample1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Product 1</name>
<sam:property xsi:type="sam:color"
xmlns:sam="http://mysample.org/sample1">red</sam:property>
</product>
---- END: console-output ----
Here comes the trouble: "property" has a namespace "sam". Since color is
defined in the same namespace like product, the usage of namespaces is
not necessary. I'd like to to see this output:
<?xml version="1.0" encoding="UTF-8"?>
<product xmlns="http://mysample.org/sample1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Product 1</name>
<property xsi:type="color">red</property>
</product>
Is there any way to achieve this? I played around using XmlOptions -
without success.
TIA,
Stefan
- ---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/