Creating an XSD schema
I have an xml tag:
<ROW field1="value 1" field2="value 2" ... />
fieldi has a string value, and number of attributes fieldi is variable, but not less than 1. Is it possible to create an xsd schema for this tag?
possible xml document
<ROWDATA>
<ROW field1="dfgdf" field2="ddfg"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd"></R开发者_JAVA技巧OW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"></ROW>
</ROWDATA>
in this xml document, which I receive from a web server, can be a variable number of attributes field (I noted them as fieldi, where i means the order of a specific attribute field) So I have, unknown number of ROW elements and unknown number of field attributes in the ROW element
Thanks
If you're using Visual Studio 2008:
- Open your Xml file in Visual Studio
- Go to the 'Xml' menu option at the top of the screen
- Choose 'Create Schema'
This will generate your xsd schema(s)
EDIT Try this example for details on setting minOccurs (on elements) or required (on attributes) so you can manipulate your derived schema.
If you are not comfortable in writing XSD yourself, use some generator like this.
EDIT: Based on your XML in comments, I can think of below structure of XSD
<xsd:element name="FieldHeader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Fields" type="xsd:string"/> <!--use minOccurs maxOccurs here-->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="fieldi">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="Fields">
<xsd:list itemType="fieldi" />
</xsd:simpleType>
I think I have understood your requirement. Just to avoid misconceptions, let me reveal what I have understood once:
"You have an xml file which contains an element with the name fieldi
, comes with set of some unknown attributes. Which means you don't know [or say don't want] the names and values of those attributes. Just want to see, there is at-least 1 attribute appearing",
Well. sorry to say that, this requirement is running out of capability of XML-schema. :-[
You cannot have attributes undeclared in schema. If it appears in xml, it requires to have a proper definition for that. There is something called <anyAttribute/>
[click-here] which again requires definition [somewhere, in another linked schema].
1) Defining all the possible attributes making use="optional"
, doesn't look practically possible. And also your last requirement go skipped.
2) If it is possible then, convert all the attributes to elements [using transformation, or you can either ask the sender to do so, I don't know how complicate it is in your case], And define element <any/>
, which sounds somewhat comfortable. but your requirement [at-least one attribute must appear] is still not achieved.
so this is it I can add-up for you. If you can change the requirement or input xml structure then let me know, I will see, whether I can help you in any-other ways ..
regards,
infant-pro
I solved the problem but in other way, by controlling the deserialization of the xml document in the way I need. However, I don't like this solution, because I had wanted to create classes from the xsd scheme and use them in my code.
Anyway, thanks to everybody
精彩评论