将 XSD 与包含一起使用

本文关键字:一起 包含 XSD | 更新日期: 2023-09-27 18:30:49

这是一个XSD:

<?xml version="1.0"?>
<xsd:schema 
elementFormDefault='unqualified' 
attributeFormDefault='unqualified' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
>
  <xsd:simpleType name='TheSimpleType'>
    <xsd:restriction base='xsd:string' />
  </xsd:simpleType>
</xsd:schema>

下面是包含上述第二个 XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema 
elementFormDefault='unqualified' 
attributeFormDefault='unqualified' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
targetNamespace='a'
xmlns='a'
>
  <xsd:include schemaLocation='Include.xsd' />
  <xsd:element name = "TheElement" >
  <xsd:complexType>
  <xsd:attribute name="Code" type="TheSimpleType" use="required"/>
  </xsd:complexType>
  </xsd:element>
</xsd:schema>

我需要将(第二个)XSD 读取到 C# 中,并且:

  1. 检查它是否是有效的 XSD,以及
  2. 根据它验证文档。

下面是一些要在架构中读取的 C#:

    XmlSchemaSet schemaSet = new XmlSchemaSet();
    foreach (string sd in Schemas)
    {
        using (XmlReader r = XmlReader.Create(new FileStream(sd, FileMode.Open)))
        {
            schemaSet.Add(XmlSchema.Read(r, null));
        }
    }
    schemaSet.CompilationSettings = new XmlSchemaCompilationSettings();
    schemaSet.Compile();

这。Compile() 失败是因为"类型 'a:TheSimpleType' 没有声明,或者不是简单类型。

但是,如果出现以下任一情况,它有效:

  • 从架构中删除命名空间,或者
  • 命名空间将添加到包含中。

问题是:如何在不编辑架构的情况下让 C# 接受它?

我怀疑问题是,尽管我已经将两个模式都放入 XmlSchemaSet 中,但我仍然需要告诉 C# 一个包含在另一个中,即它还没有自行解决。事实上,如果我只告诉 XmlSchemaSet 关于主 XSD(而不是包含)(没有(或带有)命名空间),那么"类型'TheSimpleType'没有声明,或者不是一个简单的类型。

因此,这似乎是一个关于解决包含的问题:如何?!

将 XSD 与包含一起使用

问题在于打开架构以读取的方式:

XmlReader.Create(new FileStream(sd, FileMode.Open)

我必须编写自己的XmlResolver才能看到包含文件的路径是如何解析的:它来自可执行文件的目录,而不是来自父架构的目录。问题是父架构没有获得其 BaseURI 集。以下是必须打开架构的方式:

XmlReader.Create(new FileStream(pathname, FileMode.Open, FileAccess.Read),null, pathname)
您可以使用

XmlSchema.Includes将它们链接在一起。然后,您只需要将主架构添加到架构集中:

var includeSchema = XmlSchema.Read(XmlReader.Create(...), null);
var mainSchema = XmlSchema.Read(XmlReader.Create(...), null);
var include = new XmlSchemaInclude();
include.Schema = includeSchema;
mainSchema.Includes.Add(include);
var schemaSet = new XmlSchemaSet();
schemaSet.Add(mainSchema);
schemaSet.Compile();

XmlSchemaSet 的默认行为是不尝试解析任何包含的 XSD 架构。为此,必须初始化 XmlResolver 属性。

XmlSchemaSet schemas = new XmlSchemaSet
{
    XmlResolver = new XmlUrlResolver()
};

此外,您必须根据Barraclough@Richard答案为XmlReader设置baseUri

试试这个:D

public static XmlSchema LoadSchema(string pathname)
{
    XmlSchema s = null;
    XmlValidationHandler h = new XmlValidationHandler();
    using (XmlReader r = XmlReader.Create(new FileStream(pathname, FileMode.Open)))
    {
        s = XmlSchema.Read(r, new ValidationEventHandler(h.HandleValidationEvent));
    }
    if (h.Errors.Count > 0)
    {
        throw new Exception(string.Format("There were {1} errors reading the XSD at {0}. The first is: {2}.", pathname, h.Errors.Count, h.Errors[0]));
    }
    return s;
}
public static XmlSchema LoadSchemaAndResolveIncludes(string pathname)
{
    FileInfo f = new FileInfo(pathname);
    XmlSchema s = LoadSchema(f.FullName);
    foreach(XmlSchemaInclude i in s.Includes)
    {
        XmlSchema si = LoadSchema(f.Directory.FullName + @"'" + i.SchemaLocation);
        si.TargetNamespace = s.TargetNamespace;
        i.Schema = si;
    }
    return s;
}
public static List<ValidationEventArgs> Validate(string pathnameDocument, string pathnameSchema)
{
    XmlSchema s = LoadSchemaAndResolveIncludes(pathnameSchema);
    XmlValidationHandler h = new XmlValidationHandler();
    XmlDocument x = new XmlDocument();
    x.Load(pathnameDocument);
    x.Schemas.Add(s);
    s.Compile(new ValidationEventHandler(h.HandleValidationEvent));
    x.Validate(new ValidationEventHandler(h.HandleValidationEvent));
    return h.Errors;
}

特别要注意si.TargetNamespace = s.TargetNamespace;

显然,这假定包含被指定为相对于它们所包含的架构的文件路径。

这是我编写的处理 xsd 验证的方法。希望这对某人有所帮助。

        /// <summary>
        /// Ensure all xsd imported xsd documented are in same folder as master xsd
        /// </summary>
        public XsdXmlValidatorResult Validate(string xmlPath, string xsdPath, string xsdNameSpace)
        {
            var result = new XsdXmlValidatorResult();
            var readerSettings = new XmlReaderSettings {ValidationType = ValidationType.Schema};
            readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; 
            readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
            readerSettings.Schemas.Add(null, xsdPath);
            readerSettings.ValidationEventHandler += (sender, args) =>
                {
                    switch (args.Severity)
                    {
                        case XmlSeverityType.Warning:
                            result.Warnings.Add(args.Message);
                            break;
                        case XmlSeverityType.Error:
                            result.IsValid = false;
                            result.Warnings.Add(args.Message);
                            break;
                    }
                };
            var reader = XmlReader.Create(xmlPath, readerSettings);
            while (reader.Read()) { }
            return result;
        }

使用 .net6,代码如下 -


XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.XmlResolver = new XmlUrlResolver();     // Need this for resolving include and import
settings.ValidationType = ValidationType.Schema; // This might not be needed, I am using same settings to validate the input xml
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.Schemas.Add(null, "yourpath''yourxsd.xsd");
settings.Schemas.Compile();
string xmlFilePath = "yourpath''your.xml";
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
// Create an XmlReader for the XML file
using (XmlReader reader = XmlReader.Create(xmlFilePath, settings))
{
}

想在net6中强调这一点,我们必须设置设置。Schemas.XmlResolver = new XmlUrlResolver() 而不是 settings。XmlResolver = new XmlUrlResolver()