C# 中的 XML 模式 1.1 断言

2024-03-17

我正在使用以下 xml 架构验证一些 xml 文件:

        String xsdMarkup = 
          "[...]

           <xsd:complexType name='connectionType'>
                <xsd:attribute name='SourceElement' type='guidType' use='required' />
                <xsd:attribute name='TargetElement' type='guidType' use='required' />
                <xsd:attribute name='GUID' type='guidType' use='required' />
                <xsd:assert test='@SourceElement == 0' />
           </xsd:complexType>

           [...]
          ";

        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
        Console.WriteLine("Validating doc ...");
        docToValidate.Validate(schemas, (sender, e) =>
        {
            Console.WriteLine(e.Message);
            valid = false;
        }, true);

我只是想引入一些断言标签以获得更强大的验证。但这会导致异常:

System.Xml.Schema.XmlSchemaException:http://www.w3.org/2001/XMLSchema:assert-element http://www.w3.org/2001/XMLSchema:assert-element在这种情况下不支持。

我现在不知道的是是否...

  1. 我在 xsd 中的错误位置使用了断言元素
  2. The http://www.w3.org/2001/XMLSchema-命名空间 http://www.w3.org/2001/XMLSchema-Namespace不支持 XML 模式 1.1 版,因此不支持断言元素
  3. C# XmlSchemaSet 不知道如何处理断言元素

提前感谢您的帮助!


XSD 模式的 .NET 实现仅处理版本 1.0,而不处理版本 1.1 - 因此它不支持assert.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 中的 XML 模式 1.1 断言 的相关文章

随机推荐