Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Element containing Simple Text or Children- XSD

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I have an XML Element which can take the following format
Code:
<item>
  Simple info
</item>

or

Code:
<item>
  <line>Complex Info 1</line>
  <line>Complex Info 2</line>
</item>

Is it possible to enforce/describe this structure in a XSD? if so, how? (don't worry about line, I already have it defined. I tried the following, but it doesn't allow the 'simple' type.
Code:
   <xs:complexType name=&quot;item&quot;>
        <xs:sequence minOccurs=&quot;0&quot;>
            <xs:element name=&quot;line&quot; type=&quot;xs:string&quot; maxOccurs=&quot;unbounded&quot;/>
        </xs:sequence>

    </xs:complexType>
 
hmm. its been a while.

try this:

<xs:complexType name=&quot;item&quot; content=&quot;mixed&quot;>
<xs:sequence minOccurs=&quot;0&quot;>
<xs:element name=&quot;line&quot; type=&quot;xs:string&quot; maxOccurs=&quot;unbounded&quot;/>
</xs:sequence>
</xs:complexType>

content = &quot;mixed&quot; will now allow both text and elements in the tag item.

Try reading this :
Hope that helps.

Matt
 
The description above is just what you need...
What's wrong with that, it's a perfectly legal construct. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
*thwap*

no its not. the xsd processor will default the complexType to only contain the sequence of nodes and not text if you dont put content=&quot;mixed&quot; in it.



 
Well...
I don't think that using mixed content is a good practice in this case. Some obviously non-intended elements could be OK'd by the validating parser, and you could get unwanted results.

The fragment
<item>I don't want to have this text here.
<line>Complex Info 1</line>
<line>Complex Info 2</line>
</item>

would get validated.
The mixed content is best appropriate for rendering documents - see HTML. I am not sure if in case of raw data stored in XML, the mixed content wouldn't produce confusion.

A good try would have been the UNION datatype, but that works only on simple types.

A good try IS the use of choices, but I am not sure how to do it in this case, in other means than defining two different datatypes and names for the elements, seems I've lost my inspiration.
I am looking forward for an answer on this post too. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Mixed content is probably my best option, as my line type is defined as a complex type:

Code:
    <xs:complexType name=&quot;line&quot;>
        <xs:sequence>
            <xs:element name=&quot;line&quot; type=&quot;xs:string&quot; maxOccurs=&quot;unbounded&quot;/>
        </xs:sequence>
        <xs:attribute name=&quot;flag&quot; type=&quot;xs:boolean&quot; use=&quot;optional&quot;/>
    </xs:complexType>


Hence, can't use UNION?
 
I dont think UNION would help either. The mathematical definition of a union indicates you can have both text AND nodes, and therefor would still include your example.

Think of it this way, you are only specifying what CAN be in the tag. drkestrel can build into his program the ability to ignore your example. (not the best solution, i agree).

I also agree it would be useful to have text or elements, but obviously MS have decided in their wisdom this is irrelevant :( I cant seem to find any way of specifying this anywhere in any xsd documentation, correct me if i'm wrong.
 
Just to straighten the UNION stuff...
As posted above, UNION can be used ONLY with simple datatypes and the <Item> stuff is a complex datatype, so it won't work in this case. That was just a &quot;what if that was possible&quot; stuff, sorry if I mislead you, Doctor.

However, Flumpy, you're right with your note on union, my mistake.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top