XSD: Allow any unknown element in any order

前端 未结 1 555
天命终不由人
天命终不由人 2021-01-21 10:21

I need to write an XSD schema. In this schema, some elements are known and mandatory, others are unknown and optional:


    

        
1条回答
  •  面向向阳花
    2021-01-21 10:44

    With your requirement, you have hit the Unique Particle Attribution rule, which is important enough that it has its own Wikipedia page.

    The problem is with combining any order with any element anywhere. There is no way that a processor can deterministically determine to what declaration a certain element belongs to. Does childMandatory1 belong to xs:any or does it belong to the element declaration?

    As soon as you use xs:any you can easily run into this issue. That is why a sequence is allowed to be combined with xs:any, but only if the items in the sequence are mandatory and in order, otherwise there is again no telling what element belongs to what declaration.

    If the reason you create such an XSD is to validate input for the presence of certain elements, you can switch to XSD 1.1, where you can resolve this using xs:assert, or you can use a different tool, like RelaxNg or Schematron, which are alternative standardized XML schema languages.

    If you want to create an object model out of this, you may run out of luck, because even if you manage to do so, the (de)serializer will have no way telling the declarations from each other.

    Here's an example in XSD 1.1:

    
        
            
        
        
    
    

    A good and helpful article on writing extensible XSD Schema's using Variable Content Containers can be found on XFront. While old, it still very much applies today. There's also an article on the use of xs:any.

    0 讨论(0)
提交回复
热议问题