Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to xsd. I'm trying to create a xsd so that my xml should be in the following way..

<Info>
            <Val name="n_1">A</Val>
            <Val name="n_2">123</Val>
            <Val name="n_3">2012-05-05T00:00:00</Val>          
</Info>

The xsd which I created is in this way..

<xs:element name="Info">
    <xs:complexType>
        <xs:sequence>
             <xs:element name="n_1" type="xs:string"/>
            <xs:element name="n_2" type="xs:integer"/>
            <xs:element name="n_3" type="xs:dateTime"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

This obviously did not help in meeting my requirements.. But at this point of time I'm struck about one thing .. how to create 3 elements "val" whose attribute value is different... Even if I make it somehow then i will get list of lists error.. how can I manage that?

I'm actually writing this xsd so that my data in excel can be converted to xml.. To add something about my excel, one row is a set in which one column is Info (worst thing comes here :| as I have 3 Val's for one Info) ...

I initially thought this xml is wrong but I was wrong.. it is a standard output/input xml..

Any help in achieving this would be appreciable.

Thanks in advance.. :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

The XSD should be something like this:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Info">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Val" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="name">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="n_1"/>
                      <xs:enumeration value="n_2"/>
                      <xs:enumeration value="n_3"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

It declares an element Info that contains a list of sub-elements Val. Each element Val contains a string values (the <xs:simpleContent> and <xs:extension base="xs:string">), and also a name attribute that has only three possible values n_1, n_2 and n_3.

Depending on you exact requirements you can play with the type of the Val content - specify that is a string with a certain maximum lenght for example. Similarly you can change the restrictions on the values of the name attribute (or have no restrictions)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...