user3141034
user3141034

Reputation: 161

How to fail an XML Validation by XSD

Hy, i am a newbie to XSDs and XML.I have following XML message which i am validating against a schema. I am using different online editors available for that. For example this editor.It is validating my XML structure, but my problem is that if i change the type of an element to float from string it still validates my XML structure. So i am confused that this means that my XSD is not correct.

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<credentials xmlns:cdm="http://com.example/test/current">
   <cdm:username>jawad</cdm:username>
   <cdm:password>jawad123</cdm:password>
</credentials>

XSD File:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
xmlns:cdm="http://com.example/test/current"
xmlns = "http://com.example/test/current"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xs:element name="credentials"></xs:element>

<xs:complexType name="credentials">
    <xs:sequence>
        <xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1"/>
        <xs:element name="password" type="xs:string" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

</xs:schema>

Note:

If i change the type of "username" element to "float", the editor should show the validation failed error but it is still validating it so is there something wrong with my xsd or i did not understand the concept of schema validation.

Upvotes: 1

Views: 1462

Answers (2)

Michael Kay
Michael Kay

Reputation: 163645

I think you're going to hit problems with namespaces.

You need one schema document for each namespace in the instance document (counting the "null" namespace as a namespace for this purpose), and if the namespace isn't "null", the targetNamespace attribute of the schema document must be present.

Upvotes: 0

potame
potame

Reputation: 7905

First, your schema can't work as expected, since your "credentials" element does not refer to "credentials" complexType. (When no other type is specified, the "credentials" element defaults to allowing any well-formed XML - that is why your document is valid even if "username" is declared as "float".)

It may be corrected as follows :

<xs:element name="credentials" type="credentials"/>

Then, can you explain how you validate document? Indeed, it should be processed correctly by your parser, but validation may have been turned off.

Upvotes: 2

Related Questions