XSD for Element Or OtherElement Or Text - xsd

I need an XSD for the following construct:
Expression := <FunctionCall> | <OperatorConstruct> | <Variable> | Constant_Text
In other words, the Expression type consist of a choice between 3 other types and a text.
I know there is a xs:Choice element, but I can't figure out how to write the 'Or Text' part. Simply using mixed=true on the Expression element allows to enter text AND other elements, but I would like to limit to only one from those four.
So the question is, what xsd can I define that allows one of three elements or a text?

If you want your structure validated by an XML Schema, you'll have to make all four choices into elements. MathML expressions work that way, with elements for every term.
Or you could go with mixed and validate the structure outside of XSD (with XSLT or Schematron or your own parsing code).

Related

Difference between XSD Simple element and XSD Complex element

I Googled this Question but still i'm unable to find the best difference for the Simple XSD (XML Schema Definition) Element and Complex XSD Element.
Any guidance would be highly appreciated.
I have no idea, why I answer this. But...
To summarize,
simple types can only have content directly contained between the element’s opening and closing tags. They cannot have attributes or child elements.
complex types can have attributes, can contain other elements, can contain a mixture of elements and text, etc etc.
One is a single value and the other a compound value.

XML Schema for choice between element and #PCDATA

I have a preexisting XML document type that has an element that can have two content types: some elements, or just text. Modeling this as mixed content is overkill, and JAXB's XJC creates a very ugly binding as a result.
<bars><bar .../><bar .../></bars>
versus
<bars>Just a bunch of #PCDATA</bars>
xs:choice seems structured only for complex types (not simple types like xs:string). Is there a way to express this choice, between elements or text, using XML schema? In DTD this would be something like
<!ELEMENT bars (#PCDATA | bar*)>
The language you want to define (either a sequence of character or a sequence of bar elements, but not a mixture) cannot be defined in XSD 1.0 (or in XML DTDs, either; your DTD notation would make sense but is not legal in XML DTDs).
In XSD 1.1, you can use an assertion to ensure that if any bar elements are present as children, no text nodes occur (or only text nodes that contain only whitespace).
A simple way to achieve roughly the same effect is to say that the bars element contains either a sequence of bar elements or a single stringvalue element (call it whatever you like), where the stringvalue element contains -- as its name suggests -- just a string of characters.

Given an XSD is it possible to list a hierarchy of elements and their attributes?

Lets pretend I have an XSD document and I want to produce a list of all elements along with their attributes and the children of the elements. I could also approach this by asking if you are to implement code completion based on an xsd document, and you want to list the children of the element and an elements attributes, how would you approach this problem?
Since XSD is valid XML document it just a matter of selecting XML parsing library of your choice. For example XLinq (.NET FW 3+) will do the job.
You can just walk through complexType, sequence and other elements to find out a list of possible values.

XML Schema (XSD) - if one element has specific value then another element must be present and vice versa

Can I express this in an XSD?
For example:
One element is a required bool element named EmployedMoreThanThirteenWeeks and if the value is set to false I want the schema to require the existence of another element named EmploymentDate. And the other way around if the value is true then ideally the EmploymentDate element should be denied but I can accept it being optional.
No. An XSD just defines structure and data types, not relations. It is possible to add a key reference between elements but that won't prevent invalid nodes, just invalid values.
You can create an XSLT file (an XML Stylesheet) which will validate the XML file for you and thus generate a report of errors.
I think that XSD CANT do that, because the schemas verifies just an STRUCTURE (tree), and not VALUES (though you can check the value format).
You should consider other validation ways.

How to get an ordered list parsed by XML parser?

I am using a xsd schema file; there I specified an ordered list.
When parsing an XML node of the kind...
<myOrderedList> "element_1" "element_2" "element_3" </myOrderedList>
(which is valid XML syntax)
...all XML parsers I know parse this as a single node element.
Is there a way to get the XML parser parse this list for me (return it as a list or an array or whatever) or do I always have to parse it myself?
Why not make use of XML's ability to structure your data, and put each element in it's own XML element ? e.g.
<myOrderedList>
<element>1</element>
<element>2</element>
<element>3</element>
</myOrderedList>
etc. Otherwise you're having to implement parsing (albeit in a simple fashion) on top of the parsing effort that the XML parser is performing for you ?
If you do the above, the parser will return you the ordered list without any further work, and/or you can process it more easily using standard XML tooling like XSLT/XQuery etc.
Values in a list type are always separated by whitespaces so you can easily pass that through a tokenizer to get the list of values.
There are technologies like XSLT 2.0 schema aware that will see the list of values for such an element.
Using elements as proposed in the other answer is also a solution and may ease your processing. In XML child nodes are ordered so you should not worry about that. A possible representation would be:
<myOrderedList>
<value>element_1</value>
<value>element_2</value>
<value>element_3</value>
</myOrderedList>

Resources