tags:

views:

20

answers:

2

Hello all,

I want to create an xsd schema with an xs:any element that contains an attribute.

something like this

<xs:element name="Task">
   <xs:element name="any" type="xs:anyType">
      <xs:attribute name="type" type="xs:string" />
   </xs:element>
</xs:element>

But apparently this is not a valid schema.

What I want to accomplish is an xml schema that looks like this:

<Task>
   <randomField type="bla">test</randomField>
   <anotherField type="blabla">testing</anotherField>
   .....
</Task>

So you can enter a field that must have the attribute 'type'.

The purpose of all this is to dynamically create tasks in sharepoint. So you can enter an xml like this:

<Task>
   <AssignedTo type="string">jsmith</AssignedTo>
   <Title type="string">Task1</Title>
</Task>

But I want the xml to be dynamic, so you can enter more information like duedate, priority... And the purpose of the attribute is to decide if the datatype is string or date or int, so I could format everything correctly...

thx in advance

+1  A: 

You might want to look at the XML schema tutorial

<xs:element name="Task">
   <xs:complexType>
      <xs:any minOccurs="0"/>            
    </xs:complexType>
</xs:element>
codinguser
ok, i forgot to type the xs:complextype here, but this doesn't solve the problem, I can't enter an attribute for the xs:any element..thx for your quick reply
Rise_against
If I understand right, you are trying to add an attribute to an undefined element??Do you already know before hand what specific elements can (but must not) appear in the XML? Because if you do, you can define them in the schema just like you would any other complex element and then put them between <xs:any>...</xs:any>. That way, they can be left out or included. But if you want just any arbitrary tag to be added at will, then you need to leave the <xs:any/> tag empty. You cannot specify attributes in these case. I would recommend defining the elements (if you know them).
codinguser
+1  A: 

Boring as it may be, I'd suggest you go with a different approach:

<Task>
   <Attribute name="AssignedTo" type="string">jsmith</attribute>
   <Attribute name="Title" type="string">Task1</attribute>
</Task>

It will make your life a lot easier. Not just designing the schema but also later on in dealing with the files etc.

Stijn de Witt
Yes, but when you later assign another attribute eg. <attribute name="duedate"... </attribute> the xml wouldn be valid, because duedate isn't declared in the xsd.. So I think I really need the xs:any element..
Rise_against
@Rise_against: No, I believe @Stijn means that you would always have attribute `name`. So `duedate`, `AssignedTo`, `Title` etc. would be the values of that attribute. The xml would remain valid, because you don't necessarily need to define attribute values in your schema.
jasso