Allow only other existing XML values in XSD? (xs:key and xs:keyref)

后端 未结 1 1958
青春惊慌失措
青春惊慌失措 2021-01-25 15:52

Let\'s take the following example XML:


  NiceDevice
  123


  

        
1条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-25 16:14

    Use xs:key and xs:keyref to constrain usesDevices to have a value of a device name:

    
    
      
        
          
            
              
                
                  
                  
                
              
            
            
              
                
                  
                  
                
              
            
          
        
        
          
          
        
        
          
          
        
      
    
    

    Then this XML will be valid:

    
    
      
        NiceDevice
        123
      
      
        user1
        NiceDevice
      
      
        user2
        NiceDevice
      
    
    

    But this XML,

    
    
      
        NiceDevice
        123
      
      
        user1
        NiceDevice
      
      
        user2
        BadDevice
      
    
    

    will not,

    [Error] try.xml:16:5: cvc-identity-constraint.4.3: Key 'deviceKeyRef' with value 'BadDevice' not found for identity constraint of element 'r'.

    as requested.

    Note that I've added to your XML a single root element, which is required for your XML to be well-formed.

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