tags:

views:

74

answers:

2

Hi, I have an XML i want to validate in java.

The only problem i have left is trying to write the XSD for an attribute which is actually a reference to a namespace. It's not possible to use the "<xs:attribute name="xmlns"/>" because xmlns is not allowed as the name of an attribute. Any ideas?

XML:

<header>        
<abc xmlns="www.example.org">
<user>me</user>               
</abc>
</header>

XSD:

<xs:element name="header">
<xs:complexType> 
<xs:sequence>  
 <xs:element name="abc">  
 <xs:complexType>   <xs:sequence>
     <xs:element name="user" type="xs:string" />      
 </xs:sequence>  
 </xs:complexType>  
 </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
A: 

'xmlns' is reserved as by Namespaces in XML 1.0.

Edit: If you want certain elements to belong to a namespace, you must use this namespace in your schema, too.

I need to know what the XSD should be in order for the XML to validate.i don't care what the XML represents it's always hard coded in the same way.I can't seem to write an XSD that has no warnings because of this namespace attribute.
+1  A: 

The header element doesn't have a namespace declared, so it gets the "unnamed namespace". The xmlns attribute in the abc element declares the namespace as www.example.org (without a namespace prefix). The "user" element is actually in the www.example.org namespace, since it is a child of the "abc" element, which has a declared namespace (xmlns="www.example.org).

Neither sets of elements are using namespace prefixes to disambiguate the two "types" of elements, so it is a little confusing when you look at them. It might be more clear if you were to use a namespace prefix for the www.example.org namespace in your XML, like this:

<?xml version="1.0" encoding="UTF-8"?>
<header xmlns:example="www.example.org">        
    <example:abc >
        <example:user>me</example:user>               
    </example:abc>
</header>

The XML above and the XML that you posted are equivalent, but it is a little easier to understand which elements "belong" to the "www.example.org" namespace when you use a namespace prefix.

The oXygen schema generation utility produced two schemas(below) that import each other.

Your sample XML document validates against these schemas(since they both import each other, you can reference either one when you call your validation)

First Schema file sampleSchema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:ns1="www.example.org">
  <xs:import namespace="www.example.org" schemaLocation="example.xsd"/>
  <xs:element name="header">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="ns1:abc"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Second Schema file example.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="www.example.org" xmlns:ns1="www.example.org">
  <xs:import schemaLocation="sampleSchema.xsd"/>
  <xs:element name="abc">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="ns1:user"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="user" type="xs:NCName"/>
</xs:schema>
Mads Hansen