Make XML elements required based on attribute values in XSD

前端 未结 1 701
刺人心
刺人心 2020-12-11 09:48

My requirement is to have an XSD file which checks the elements based on attribute values. I was able to write XSD up to a point where I can restrict the attribute values of

1条回答
  •  Happy的楠姐
    2020-12-11 10:42

    Update: OP has edited question to remove the use of xs:assert and has stated in comments that validation must take place in C#. The answer to OP's question now becomes:

    You cannot enforce a constraint that varies the requiredness of an element based on an attribute value using XSD 1.0, and Microsoft does not support XSD 1.1, therefore you must either relax your constraints or check them outside of your XSD.


    Original XSD 1.1 answer

    (Retained for benefit of future readers)

    You're close, but your assertion,

          
    

    tests for @type on PackageArg when it should test for @Type on Application.

    The following XSD will validate your XML and enforce your attribute-dependent requirements:

    XSD 1.1

    
    
      
        
          
            
              
                
                  
                  
                  
                  
                  
                
                
                
                  
                    
                      
                      
                      
                    
                  
                
                
                
                
              
            
          
        
      
    
    

    Be aware that Microsoft does not support XSD 1.1. (You've tagged your question with 'msxml').

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