XSD how to have maxOccurs to value of previous element count
Hi i need to create an xsd for a xml where there are many elements of name columns, and rows.
Kindly suggest the XSD, i have made cols as unbounded how should i make the element rows.
<cols>
<id>a</id>
<type>string</type>
</cols>
<cols>
<id>b</id>
<type>开发者_StackOverflow社区string</type>
</cols>
<cols>
<id>c</id>
<type>number</type>
</cols>
<rows>
<c>
<v>a</v>
</c>
<c>
<v>b</v>
</c>
<c>
<v>3</v>
</c>
</rows>
<rows>
<c>
<v>c</v>
The xml sample you posted as it is will not be valid against any schema. In-fact xml should never have multiple root nodes (as your example does). You need to wrap these in a single root node.
In answer to your question xsd does not support attribute values conditional on a count of the number of nodes in a node-set like you assume. The best you can do is use unbounded for your maxOccurs.
The schema you want will look similar to this:
<xs:schema xmlns="http://mynamespace" targetNamespace="http://mynamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="cols">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string" />
<xs:element name="type" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="rows">
<xs:complexType>
<xs:sequence>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element name="v" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Hope this helps you
精彩评论