tags:

views:

143

answers:

4

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"></ROW>
  <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

A: 

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>
Ravi Gupta
I had tried to use some generators, but they generated attributes with the flag "optional". In my case I don't know how many attributes fieldi can be in the tag <ROW ... />
Nikolai
If you don't know how many times an element will appear in XML, you can set the maxOccurs attribute of the "fieldi" element to "unbounded" which means that there can be as many occurrences of the "fieldi" element as the author wishes.
Ravi Gupta
there is no possibility to set maxOccurs for attributes, only for elements yo can do this
Nikolai
OK, let us get this straight. Add some sample xml in your question and we will figure out a XSD for it.
Ravi Gupta
example:<ROWDATA> <ROW field1="dfgdf" field2="ddfg"> </ROW> <ROW field1="dfedf" field2="djkfg" field3="cdffd"> </ROW> <ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"> </ROW> </ROWDATA>
Nikolai
@Nikolai - could you edit your original question with the example XML and any notes about the structure you can think of. Thanks.
amelvin
+2  A: 

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.

amelvin
I had tried, but recieved a schema with "optional" attributes, this is not what I need, because I don't know how many attributes I have
Nikolai
@Nikolai - I've extended my answer.
amelvin
I have tried to use "ref" for referencing attributes, but still unsuccessful...
Nikolai
A: 

Hi Nikolai,

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

infant programmer
Hi,There is now possibility to change attributes to elements, because this is not my xml, I receive it from a server...thanks anyway
Nikolai
A: 

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

Nikolai