Shashank K
Shashank K

Reputation: 1

how to add validation in xsd for blank spaces

i have created basic xsd successfully however I want to add restriction for the element that it should be present and contains atleast one character. it also has 4 attributes. i am facing problem in adding restriction since I can not use simple type as the element has attributes.

Please suggest something

thanks in advance

Added XSD data posted by OP in comments (sic)

<xs:element name="Engines">
  <xs:complexType>
    <xs:sequence>
      <xs:element maxOccurs="unbounded" ref="Engine" />
    </xs:sequence>
    <xs:attribute name="Count" use="required" type="xs:integer" />
  </xs:complexType>
</xs:element>
<xs:element name="Engine">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Model" type="Model"/>
      <xs:element ref="SerialNumber" />
    </xs:sequence>
  </xs:complexType>
</element>

<xs:simpleType name="trimValueType">
  <xs:restriction base="xs:string">
    <xs:minLength value="1"></xs:minLength>
    <xs:whiteSpace value="collapse"></xs:whiteSpace>
  </xs:restriction>
</xs:simpleType>
<xs:complexTyp‌​e name="Model">
  <xs:simpleContent>
    <xs:extension base="trimValueType">
      <xs:attribute name="ATTRIBUTE" use="required" />
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>


<Engines count = 1> <Engine> <Model ATTRIBUTE = "r\w"> </Model> <SerialNumber ATTRIBUTE = "r/w">1234567</SerialNumber> <Engine> <Engines>

Upvotes: 0

Views: 2675

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

You have to first create a simple type that restricts xsd:string to specify your text constraints. Then you need to define a complex type, with a simple content, which extends the simple type you just created using the attributes you want. I threw in a whitespace constraint, just to match your title, even though you're not specifically mention it in your problem statement.

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="one">
        <xsd:restriction base="xsd:string">
            <xsd:whiteSpace value="collapse"/>
            <xsd:minLength value="1"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:complexType name="two">
        <xsd:simpleContent>
            <xsd:extension base="one">
                <xsd:attribute name="one"/>
                <xsd:attribute name="two"/>
                <xsd:attribute name="etc"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:element name="root" type="two"/>
</xsd:schema>

Sample XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" one="anySimpleType" two="anySimpleType" etc="anySimpleType" xmlns="http://tempuri.org/XMLSchema.xsd">root1</root>

Upvotes: 1

Related Questions