开发者

How to define element that has attributes and internal nodes?

For the following XML file

<MyElement>
    <Data1>123</Data1>
    <Data2>234</Data2>
</MyElement>

XSD Schema looks like this:

<xs:element name="MyElement"  minOccurs="1" maxOccurs="1" >
    <xs:complexType>
        <xs:sequence>
            <xs:attribute name="Data1" type="xs:unsignedInt" />
            <xs:attribute name="Data2" type="xs:unsignedInt" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

For the element that has data not in child nodes, but in attributes:

<MyElement Data1="123" Data2="234" />

XSD Schema looks like this:

<xs:element name="MyElement"  minOccurs="1" maxOccurs="1" >
    <xs:complexType>
        <xs:attribute name="Data1" type="xs:unsignedInt" />
        <xs:attribute name="Data2" type="xs:unsignedInt" />
    </xs:complexType>
</xs:element>

But how to define XSD schema for the 'mixed' case?

<MyElement Data1="123">
    <Data2>234</Data2>
</MyElement>

EDIT: and 开发者_Go百科another one:

<MyElement Data1="123">234</MyElement>

Thanks a lot!

P.S. I've tried different combinations, but validator (build-in to MSVS2010 complain on any of my ideas).


As per an online converter

<MyElement Data1="123">
    <Data2>234</Data2>
</MyElement>

converts to

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="MyElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data2" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
      </xs:sequence>
      <xs:attribute name="Data1" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="MyElement" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I guess you want just this

  <xs:element name="MyElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data2" type="xs:string" />
      </xs:sequence>
      <xs:attribute name="Data1" type="xs:string" />
    </xs:complexType>
  </xs:element>

Also your first example looks wrong as your xml nodes should be convert over to elements not attributes of the sequence

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜