Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义属性扩展现有XSD模式_C#_.net_Xml_Xsd - Fatal编程技术网

C# 使用自定义属性扩展现有XSD模式

C# 使用自定义属性扩展现有XSD模式,c#,.net,xml,xsd,C#,.net,Xml,Xsd,我需要向xml文件添加一些自定义属性,该属性由现有的、已经定义的模式验证 给定现有xml元素: <existingElement attr1="1" attr2="2" /> 使用现有xsd架构进行验证,我希望能够在不破坏流程中的架构验证的情况下向该元素添加自定义属性: <existingElement attr1="1" attr2="2" xmlns:my="http://example.com/node" my:id="myNodeId" /> 我无法修改原始模

我需要向xml文件添加一些自定义属性,该属性由现有的、已经定义的模式验证

给定现有xml元素:

<existingElement attr1="1" attr2="2" />
使用现有xsd架构进行验证,我希望能够在不破坏流程中的架构验证的情况下向该元素添加自定义属性:

<existingElement attr1="1" attr2="2" xmlns:my="http://example.com/node" my:id="myNodeId" />
我无法修改原始模式文件,它确实有anyAttribute元素

我应该采取什么方法

我已尝试添加另一个xsd文件:

<xs:schema 
    targetNamespace="http://example.com/node"
    attributeFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:attribute name="id" type="xs:string" />
</xs:schema>
但我一直在得到属性'http://example.com/node:id”“没有宣布


我正在.NET中使用添加了新旧模式的XmlSchemaSet进行验证。

在某种程度上,您必须修改现有模式,因为您试图创建的实例文档违反了其约束之一

如果原始模式使用命名的复杂类型定义结构,则可以使用以下两种机制之一修改现有模式,而无需对模式文档的文本进行文本更改:xs:redefines,或使用扩展派生定义新的复杂类型。这两种方法都在不修改模式文档的情况下修改模式:因此,这在一定程度上取决于您不修改模式的要求是什么意思。请注意,如果您使用扩展派生,那么您的实例将必须通过包含一个xsi:type属性来标记它使用扩展,该属性命名扩展的复杂类型

如果您对修改模式的关注是关于分叉XSD源定义,那么定义修改后的模式的另一种方法是将XSLT转换应用于原始模式。这实际上与xs:redefined非常相似,只是您可以随意更改,而xs:redefined约束您可以更改的内容

另一方面,如果您关心的是确保符合新模式的文档仍然与使用旧模式的应用程序兼容,那么所有这些机制都会失败:在所有情况下,您正在创建的文档不符合现有架构,因此有可能破坏依赖文档对该架构有效的应用程序。

test.xml

<?xml version="1.0" encoding="utf-8"?>
<existingElement attr1="1" attr2="2" xmlns:my="http://example.com/node" my:id="myNodeId" />

它起作用了。没有验证错误。

我回家后会尝试一下。我承认我没有做小的修改,这是一个错误。我的要求是将这样的属性添加到模式中的每个元素。这是我可以轻松做到的吗?最简单的方法可能是在XSD 1.1中使用xs:openContent,但这取决于您是否能够使用XSD 1.1。如果没有这一点,我认为这将是不容易的。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="existingElement">
    <xs:complexType>
      <xs:attribute name="attr1" type="xs:unsignedByte" use="required" />
      <xs:attribute name="attr2" type="xs:unsignedByte" use="required" />
      <xs:anyAttribute />
    </xs:complexType>
  </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://example.com/node"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:attribute name="id" type="xs:string" />
</xs:schema>
var schemaSet = new XmlSchemaSet();
schemaSet.Add("", "test1.xsd");
schemaSet.Add("http://example.com/node", "test2.xsd");
schemaSet.Compile();

var xml = XDocument.Load("test.xml");
xml.Validate(schemaSet, (o, e) =>
{
    Console.WriteLine(e.Severity + ": " + e.Message);
});