I'm trying to create XSD for small XML which has default namespace as <POSLog xmlns="http://www.nrf-arts.org/IXRetail/namespace/">
in the root element. Under the root element there's <object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="A">
namespace as well with custom xsi:type
. Following are the XML and XSDs.
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<POSLog xmlns="http://www.nrf-arts.org/IXRetail/namespace/">
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="A">
<elt1>test1</elt1>
<elt2>test2</elt2>
<elt3>test3</elt3>
</object>
<SequenceNumber xmlns:ns2="http://www.nrf-arts.org/IXRetail/namespace/" xmlns="">3</SequenceNumber>
</POSLog>
object.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.nrf-arts.org/IXRetail/namespace/" targetNamespace="http://www.nrf-arts.org/IXRetail/namespace/" attributeFormDefault="unqualified" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="schema1.xsd" />
<xs:element name="POSLog">
<xs:complexType>
<xs:sequence>
<xs:element ref="object" />
<xs:element ref="SequenceNumber" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
schema1.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="object">
<xs:sequence>
<xs:element name="elt1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="A">
<xs:complexContent>
<xs:extension base="object">
<xs:sequence>
<xs:element name="elt2" type="xs:string"/>
<xs:element name="elt3" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="SequenceNumber" type="xs:unsignedByte" />
<xs:element name="object" type="object" />
</xs:schema>
When I try to validate XSD I'm getting following error. The reason for that error because <object>
element is also trying to validate using default namespace.
ERROR: Element '{http://www.w3.org/2001/XMLSchema}schema': No matching global declaration available for the validation root.
My requirement is to generate valid XML Schemas for above sample XML.
CodePudding user response:
To adjust your XSDs to validate your XML:
Observe that
POSLog
andobject
are in the"http://www.nrf-arts.org/IXRetail/namespace/"
namespace andSequenceNumber
is in no namepace.Therefore, group your definitions such that
POSLog
andobject
are in one XSD (object.xsd
) andSequenceNumber
is in another (schema1.xsd
).Redistribute your type definitions accordingly.
Below is a complete set of adjustments needed to your XSDs to validate your XML:
Original XML
<?xml version="1.0" encoding="UTF-8"?>
<POSLog xmlns="http://www.nrf-arts.org/IXRetail/namespace/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nrf-arts.org/IXRetail/namespace/ object.xsd">
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="A">
<elt1>test1</elt1>
<elt2>test2</elt2>
<elt3>test3</elt3>
</object>
<SequenceNumber xmlns:ns2="http://www.nrf-arts.org/IXRetail/namespace/" xmlns="">3</SequenceNumber>
</POSLog>
object.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://www.nrf-arts.org/IXRetail/namespace/"
targetNamespace="http://www.nrf-arts.org/IXRetail/namespace/"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="schema1.xsd"/>
<xs:element name="POSLog">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns2:object" />
<xs:element ref="SequenceNumber" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="object" type="ns2:object" />
<xs:complexType name="object">
<xs:sequence>
<xs:element name="elt1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="A">
<xs:complexContent>
<xs:extension base="ns2:object">
<xs:sequence>
<xs:element name="elt2" type="xs:string"/>
<xs:element name="elt3" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
schema1.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SequenceNumber" type="xs:unsignedByte" />
</xs:schema>
CodePudding user response:
I found the reason for above error.
In the object.xsd I have added elementFormDefault="qualified"
attribute as this and changed the element object type as well.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.nrf-arts.org/IXRetail/namespace/" targetNamespace="http://www.nrf-arts.org/IXRetail/namespace/" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="schema1.xsd" />
<xs:complexType name="A">
<xs:sequence>
<xs:element name="elt1" type="xs:string"/>
<xs:element name="elt2" type="xs:string"/>
<xs:element name="elt3" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="POSLog">
<xs:complexType>
<xs:sequence>
<xs:element name="object">
</xs:element>
<xs:element ref="SequenceNumber" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>