【发布时间】:2021-07-05 09:23:15
【问题描述】:
为了在 C# 应用程序中验证不同的 XML 文件,我必须处理多个 XML 模式。所有这些 XML 模式都共享一些基本元素,如果不用每次都重新定义它们,将它们放在一个共同的地方就好了。
我的想法是创建一个 BaseSchema.xsd 文件来定义这些通用元素,并创建一个 SpecificSchema#.xsd 文件来定义特定的案例相关元素。在这些SpecificSchema#.xsd 架构中,我需要使用基本架构的一些元素,还要重新定义根标记。
这是我需要的一个例子:
BaseSchema.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="BaseSchema"
targetNamespace="http://company.com/myBaseSchema"
elementFormDefault="qualified"
xmlns="http://company.com/myBaseSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myRootTag" type="myRootTag" />
<xs:complexType name="myRootTag">
<!-- some content -->
</xs:complexType>
<xs:complexType name="someCommonTag">
<!-- some content -->
</xs:complexType>
</xs:schema>
SpecificSchema1.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SpecificSchema1"
targetNamespace="http://company.com/myBaseSchema"
elementFormDefault="qualified"
xmlns="http://company.com/myBaseSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="BaseSchema.xsd"/>
<xs:redefine schemaLocation="BaseSchema.xsd">
<xs:complexType name="myRootTag">
<xs:complexContent>
<xs:extension base="myRootTag">
<!-- some content -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
<xs:complexType name="someCustomTag">
<xs:sequence>
<!-- some content -->
</xs:sequence>
</xs:complexType>
</xs:schema>
SpecificSchema2.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SpecificSchema2"
targetNamespace="http://company.com/myBaseSchema"
elementFormDefault="qualified"
xmlns="http://company.com/myBaseSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="BaseSchema.xsd"/>
<xs:redefine schemaLocation="BaseSchema.xsd">
<xs:complexType name="myRootTag">
<xs:complexContent>
<xs:extension base="myRootTag">
<!-- some other content different from SpecificSchema1.xsd -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
<xs:complexType name="someOtherCustomTag">
<xs:sequence>
<!-- some content -->
</xs:sequence>
</xs:complexType>
</xs:schema>
我需要验证的 XML 文件是这样的(它们永远不会引用BaseSchema.xsd):
<?xml version="1.0" encoding="utf-16" ?>
<myRootTag xmlns="http://company.com/myBaseSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://company.com/myBaseSchema SpecificSchema1.xsd">
<!-- some content -->
</myRootTag>
显然这似乎有效(所有模式都在同一个目录中,并且它们都引用相同的 targetNamespace),但是当我尝试将所有模式加载到 System.Xml.Schema.XmlSchemaSet 中时,它会引发此异常:
'SchemaLocation' must successfully resolve if <redefine> contains any child other than <annotation>.
我还尝试在覆盖架构中引用 BaseSchema.xsd 的完整路径,但没有成功。
这是我用于模式加载的 C# 代码:
private XmlSchemaSet LoadSchemas()
{
var schemaSet = new XmlSchemaSet();
foreach (var schemaPath in Directory.GetFiles(schemasDirectory))
{
var schema = LoadSchema(schemaPath);
schemaSet.Add(schema);
}
return schemaSet;
}
private XmlSchema LoadSchema(string uri)
{
using (var reader = XmlReader.Create(uri))
{
return XmlSchema.Read(reader, null);
}
}
我做错了什么?
谢谢。
编辑:我还尝试仅在 SpecificSchema#.xsd 模式中定义根标签,而不是在基本模式中,因此不需要重新定义,但在创建阅读器来验证我的 XML:
The global element 'http://company.com/myBaseSchema:myRootTag' has already been declared.
【问题讨论】:
-
您可能需要包含语句:w3schools.com/xml/el_include.asp
-
@jdweng 我已经有
<xs:include schemaLocation="BaseSchema.xsd"/>。只写我的 XSD 基本文件的名称可以吗? (所有模式都在同一个文件夹中) -
通常您将包含放在根架构中以包含子架构。您的 c# 代码将引用根架构,并且将使用根中的包含找到子架构。
-
@jdweng 所以在我的
BaseSchema.xsd中,我应该将我的派生模式的包含放在SpecificSchema#.xsd中,然后将重新定义留在SpecificSchemas#.xsd中?它似乎不起作用,当我加载SpecificSchema#.xsd架构时,它说“'重新定义'元素不能出现在这个位置”。 -
架构由组织生成,用于标准化现实行业等数据传输,并且通常在主架构中包含用户定义的类型。假设用户编辑主模式以添加新类型来代替用户定义的类型。将 Include 语句添加到 Main Schema。然后为用户类型创建一个新模式。这最大限度地减少了用户在发布新版本架构时需要进行的更改。