xs:key, why is validation passing when key value is not member of key reference?

前端 未结 2 1358
不思量自难忘°
不思量自难忘° 2021-01-20 06:12

I am interested in defining a key constraint in my Xsd. It is my understanding that using xs:key should constrain the value used to a member of a referenced list of values.<

2条回答
  •  时光说笑
    2021-01-20 06:51

    As usual, after a good night's rest and a fresh look, spotted no fewer than 2 errors in this exercise.

    1. First error, validating identity constraints is an explicit process, induced via XmlSchemaValidationFlags.ProcessIdentityConstraints set by XmlReaderSettings.ValidationFlag, and
    2. second error, msdn sample contains an error in schema, should read .

    Full working sample is as follows,

    
      
        
          
            
              
                
                
                
              
            
            
          
        
        
          
          
        
      
    
      
        
          
            
              
                
                  
                
              
            
          
        
      
    
      
        
          
            
              
                
                  
                
              
            
          
        
      
    
    

    Xml sample

    
      
        
        
      
      
        
        
        
      
    
    

    simple validator

    [TestMethod]
    public void Test_Schema()
    {
        string schemaFileName = @"sampleSchema.xsd";
        string xmlFileName = @"sampleXml.xml";
    
        XmlSchema schema = 
            XmlSchema.Read(
            File.OpenText(schemaFileName), 
            (o, e) => { throw new Exception("BOOM"); });
    
        XmlReaderSettings settings = new XmlReaderSettings
        {
            ValidationType = ValidationType.Schema,
            ValidationFlags = 
                XmlSchemaValidationFlags.ProcessInlineSchema | 
                XmlSchemaValidationFlags.ProcessSchemaLocation | 
                XmlSchemaValidationFlags.ReportValidationWarnings | 
    
                // d'oh! explicit flag for processing identity constraints!
                XmlSchemaValidationFlags.ProcessIdentityConstraints,
        };
        settings.Schemas.Add(schema);
        settings.ValidationEventHandler += 
            (o, e) => { throw new Exception("CRASH"); };
    
        XmlReader reader = XmlReader.Create(xmlFileName, settings);
        while (reader.Read()) { }
    }
    

提交回复
热议问题