Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

Restricting XML Elements Based on Another Element via XSD

I believe this has to do with keyref but I'm not for sure, and I am really not sure that it can be done at all.

For example, say I have myElement1 and myElement2. If there are no myElement2 in the XML file, then myElement1 must exist, otherwise it is optional.

Is there any way to force this type of validation in my XSD file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Similar questions have been asked several times .. in stackoverflow or any tech blogs ..

but the answer is always not possible,


cases like :

(a) validating Element depending on the value or presence of any other element/attribute
(b) validating value of an element depending on the value or presence of any other element/attribute
(c) validating attribute depending on the value or presence of any other element/attribute
(d) defining elements/attributes of same name having different Types/DataTypes under same scope
(e) validating child elements on the basis of value or presence of any other Elements/Attributes

all the above mentioned cases are not possible to achieve ..

but if you can implement some transformation thing .. then you can get help. what I mean to say is "first go through transformation and modify the XML as per your requirement,

example :: if the particular field is expected with some value .. pass the XML as it is if the condition is satisfied and don't pass if it doesn't .. in the next block XSD is going to validate and obviously errors out :) you can figure out that it is missing your custom validation ..

anyway the intention is to block or Pass the xml according to the data .. and that is what you are doing ..

Note: Schematron has been come into good practice recently! Here is the tutorial: http://zvon.org/xxl/SchematronTutorial/General/contents.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...