Justin Zayne
Justin Zayne

Reputation: 33

How to use targetNamespace in XSD to validate XML

i am now officially lost on namespaces. I am trying to validate XMLs that we get from customers in this (very abbreviatet) format:

<?xml version="1.0"?>
<order xmlns="http://dev.bla.de/schema/order/1.2">
  <order-number>400056980</order-number>
</order>

i am trying to create a matching Schema for this and i came up with:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://dev.bla.de/schema/order/1.2" xmlns:bla="http://dev.bla.de/schema/order/1.2" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="order" type="bla:someOrder"/>

<xs:complexType name="someOrder">
  <xs:sequence>
    <xs:element maxOccurs="1" minOccurs="1" name="order-number">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:minLength value="1"></xs:minLength>
          <xs:maxLength value="13"></xs:maxLength>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

</xs:schema>

but when i validate with xmllint i get:

example.xsd:2: element schema: Schemas validity error : Element '{http://www.w3.org/2001/XMLSchema}schema': No matching global declaration available for the validation root.
example.xsd fails to validate

How exactly do i have to write this XSD to start validating this XML? I am officially lost here, i think it's an issue with the targetNamespace, but after 2 days of banging my head against this i am not sure about anything anymore...

Please help! ;)

SOLUTION:

Thx to deveth0 the head of the schema has to be altered to look like this:

<xs:schema targetNamespace="http://dev.bla.de/schema/order/1.2" xmlns="http://dev.bla.de/schema/order/1.2" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

  <xs:element name="order" type="someOrder" />

Upvotes: 3

Views: 2291

Answers (1)

deveth0
deveth0

Reputation: 508

i got this to validate by using a named namespace:

<?xml version="1.0" encoding="UTF-8"?>
<bla:order xmlns:bla="http://dev.bla.de/schema/order/1.2">
  <order-number>400056980</order-number>
</bla:order>

Upvotes: 1

Related Questions