Need assistance in creating an xsd

[亡魂溺海] 提交于 2019-12-25 17:18:46

问题


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.. :)


回答1:


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)




回答2:


Try this XSD.This enforces uniqueness of "name" attribute of Val nodes.

<?xml version="1.0" encoding="utf-8"?> <xs:schema   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:anySimpleType">
                                    <xs:attribute name="name" use="required" />
                                </xs:extension>
                            </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueNameForValList">
        <xs:selector xpath="Val" />
        <xs:field xpath="@name" />
    </xs:unique>
</xs:element> 




回答3:


Try this XSD

<?xml version="1.0"?>
<xs:schema id="Info" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Info" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="Val" nillable="true">
      <xs:complexType>
        <xs:simpleContent msdata:ColumnName="Val_Text" msdata:Ordinal="1">
          <xs:extension base="xs:string">
            <xs:attribute name="name" type="xs:string" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:choice>
</xs:complexType>



来源:https://stackoverflow.com/questions/10250104/need-assistance-in-creating-an-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!