0
votes

Consider my xml is like this.It contain a root tag employee and 2 child tags both are string type

<employee>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</employee>

and the corresponding xsd like this

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string" minOccurs="1"/>
      <xs:element name="lastname" type="xs:string"minOccurs="1"/>
    </xs:sequence>
  </xs:complexType>
</xs:element> 

I want a validation that the first name and last name should be string and it should occur.When l gave type="xs:string" and minOccurs="1" and gave first name tag like this (ie.no value b/w the tag) it shows no error...But I want to show an error if there is no value b/w the tag.What I should do? Could you please give me a solution as soon as possible?

1

1 Answers

2
votes

I hope that I understood you well, your problem is, that

<employee>
  <firstname></firstname>
  <lastname>Smith</lastname>
</employee>

is valid according to XSD.

First of all you have to realize that in many languages empty string and null value are not the same, for example in Java:

String s1 = "";   // empty string, length is 0
String s2 = null; // null value, has no length...

That's not true for Oracle PL/SQL where null and empty string are the same.

So I guess that this valid XML (according your XSD) is not ok for you too:

<employee>
    <firstname xsi:nil="true" />
    <lastname xsi:nil="true" />
</employee>

If you have additional conditions that firstname/lastname have to meet, you have to create own type:

<xs:element name="employee">
    <xs:complexType>
        <xs:sequence>
            <!-- changed -->
            <xs:element name="firstname" type="e:firstnameType" />
            <!-- not changed -->
            <xs:element name="lastname" type="xs:string" minOccurs="1" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:simpleType name="firstnameType">
    <xs:restriction base="xs:string"> 
        <xs:whiteSpace value="collapse" />
        <xs:minLength value="1" />
    </xs:restriction> 
</xs:simpleType>

Nice description of collapse is here. Other restrictions for string you can find here.

For others I'm adding my complete XSD and XML files:

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:e="employee"
    targetNamespace="employee"
    elementFormDefault="qualified">
    <xs:element name="employee">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="firstname" type="e:firstnameType" />
          <xs:element name="lastname" type="xs:string" minOccurs="1"  />
        </xs:sequence>
      </xs:complexType>
    </xs:element>

    <xs:simpleType name="firstnameType">
        <xs:restriction base="xs:string"> 
            <xs:whiteSpace value="collapse" />
            <xs:minLength value="1" />
        </xs:restriction> 
    </xs:simpleType>
</xs:schema>

XML

<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns="employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <firstname>Martin</firstname>
  <lastname xsi:nil="true"></lastname>
</employee>