0

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>
Shashank K
  • 1
  • 1
  • 6

1 Answers1

1

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>
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • i have tried this and getting exception - The content of elements must consist of well-formed character data or markup. – Shashank K Apr 13 '12 at 14:55
  • @Shashank, what did you try, and how (as in what tool did you use)? Can you post the XML you've tried? I've just updated the post with a sample XML that validates. – Petru Gardea Apr 13 '12 at 15:01
  • I am validating the xml created with your suggestion using java SAXParser. it is validating properly if element value is blank or spaces however system is throwing exception at the end of the document like cvc-complex-type.2.2: Element 'RecordTransferFile' must have no element [children], and the value must be valid. – Shashank K Apr 13 '12 at 15:13
  • continue xsd: – Shashank K Apr 13 '12 at 15:21
  • @ShashankK it is much easier if you edited your original question to addd the new XSD file @PetruGardea requested. I have done so for you. Not sure if this is a typo in your response or your real XSD file, but your element `Engine` was not closed. you have `` when you should have `` – psubsee2003 Apr 13 '12 at 17:13
  • @ShashankK, you're saying about an error re:RecordTransferFile yet there's no XSD fragment to show that... Can you put an XML and the error in the context of the XSD associated with your post? – Petru Gardea Apr 14 '12 at 03:07
  • Sorry but i don't know how to add xsd or xml in the question sample xml: 1234567 – Shashank K Apr 14 '12 at 06:34
  • @ShashankK you just click edit under your question and add the information. the best way to format it is to type or paste it in then hit the `code block` option ( looks like {} ) – psubsee2003 Apr 14 '12 at 11:12
  • @ShashankK, update the post with the complete XSD as well; you're missing the serialnumber element definition, etc. – Petru Gardea Apr 15 '12 at 05:39