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

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.

Related

How can I write an XSD which doesn't care what the root element is called?

My XML files have restrictions on the child elements, but it really doesn't matter what the name of the root element is. How can I incorporate this into my XSD? I've tried using <xs:any> but I get:
"S4s-elt-invalid-content.1: The Content Of 'schema' Is Invalid. Element 'any' Is Invalid, Misplaced, Or Occurs Too Often."
So I tried missing the name off the element tag like this: <xs:element> but then I get:
"S4s-att-must-appear: Attribute 'name' Must Appear In Element 'element'."
Use a named type, and tell your validator to start validation at the root element using that type.
(There is one possible hitch with this: XSD 1.0 suggests that as one possible invocation option, but does not require validators to provide it, so there's no guarantee the validator interface you use will support it. Depends on your validator. Worth trying, at least.)
Another way to put this: you already have what you are asking for, because your XSD schema never cares what the root element of your document instance is called. An XSD schema provides a set of element and type declarations (among other things). A validator can be requested to start the validation at any point in the document, not just the root, and with either an element declaration or a type declaration, or in 'lax wildcard mode' (the most common default). If your validator doesn't offer the invocation options you want, it's a flaw in your choice of validator, not a gap in XSD.
I think I might just make the requirement stricter and insist on using a particular tag as the root element. The fact that the application doesn't care is not really a problem.
It seems (to me) strange that this limitation exists, but I am new to XSDs.

how to write xml schema(XSD)

I want to write one XML schema(XSD)
in which
element have multiple occurences and have one attribute ID
I want validate if borrower's ID attribute have a value equal to 1 then all its subelement must have some value.
is this possible with XSD?
Please suggest me if their is a way to achieve this
thanks
Conditional's like you are requesting are not possible in XML schema's. You might want to look at RELAX NG(http://relaxng.org/).

how to write xml schema(XSD)

I want to right one XML schema(XSD)
in which
element have multiple occurences and have one attribute ID
I want validate if borrower's ID attribute have a value equal to 1 then all its subelement must have some value.
is this possible with XSD?
Please suggest me if their is a way to achieve this
thanks
Not possible as far as I can remember, but it is totally OK to first validate the XML using XSD and then have some additional, application-specific validation logic on top of that.

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.

Handling XSD element declared twice

I have an XML Schema referencing two other schemas where the same element is declared twice resulting in an invalid schema.
Is there any built in XSD construct allowing me to handle this situation by ignoring one occurrence or is this situation just fundamentally wrong?
That's fundamentally wrong - a schema is specifically intended to make things clear and unique. You need to address that somehow - XSD has no way of ignoring something that's in the schema - anything in the schema file must be valid.

Resources