简体   繁体   中英

Conditions in XSD for validating XML

Does XML need to be strictly validated against XSD? How to validate condition based XML in XSD?

I have a scenario where I need to validate a condition. After doing some research I found that XSD 1.0 won't do condition based verification.

Example: based on an attribute 'Y' or 'N', I have to display different elements.

Please provide any suggestions.

Consider the following design: inside our A element, we have a frob attribute with the value 'Y' or the value 'N'. If frob='Y' , then A must contain B , C , and zero or more occurrences of D ; if frob='N' , then the contents of A are X , Y , Z . As you say, XSD 1.0 cannot enforce the consistency of the frob attribute with the contents of A .

Now consider the following design: the A element contains either B and C followed by zero or more occurrences of D , or else X , Y , and Z in that order. XSD 1.0 can express this design very simply.

For every XML instance correct against the first design, there is a corresponding instance which is correct against the second design, and vice versa. In other words, the two designs contain exactly the same information.

Under these circumstances, my suggestion would be to ask yourself why you want the redundant frob attribute, when it conveys no useful information not already conveyed by the content of A .

No, XML does not have to be validated against XSD.

XSD 1.0 is not capable of enforcing arbitrary constraints on your data. If you have constraints that cannot be enforced using XSD 1.0, you have a number of choices: redesign the XML, move to XSD 1.1, validate it using a different technology (including the option of writing your own custom validator, say in XSLT), or not validating it at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM