-1

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

4

3 回答 3

1

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> 

于 2012-04-20T19:08:02.593 回答
1

XSD 应该是这样的:

<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>

它声明了一个Info包含子元素列表的元素Val。每个元素都Val包含一个字符串值(<xs:simpleContent><xs:extension base="xs:string">),以及一个name只有三个可能值的属性n_1n_2n_3

根据您的确切要求,您可以使用Val内容的类型 - 例如,指定它是一个具有特定最大长度的字符串。同样,您可以更改对name属性值的限制(或没有限制)

于 2012-04-20T16:56:20.980 回答
0

试试这个 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>

于 2012-04-20T16:54:20.053 回答