调整 xsd 验证以部分忽略顺序

发布于 2025-01-01 04:21:01 字数 1774 浏览 3 评论 0原文

我有一个 xml 查询,应该经过 xsd 验证...(不可能??)

不幸的是,xml 的 xsd 架构包含 xsd:两个序列之间的选择。

但我在 xml 查询中指定了这两个序列。

这导致 xsd 无法通过 xml,请问我如何覆盖此检查?

提供:

1. XSD 模式是标准化的无法更改。

2. 任何更改都必须在我应用验证的 C# 代码中完成。

同样的问题也被问到:

Ignore element order while validating XML against XSD< /a>

但答案是修改 xsd 架构,这是用户接受的答案。

update1

xml 与此类似:

...
<elem>
    <subElemA>textValA</subElemA>
    <subElemB>textValA</subElemB>
<elem>
...

但 XSD 表示:

...
<xsd:choice>
    <xsd:element name="subElemA" minOccurs="0" maxOccurs="1">
    </xsd:element>
    <xsd:element name="subElemB" minOccurs="0" maxOccurs="1">
    </xsd:element>
</xsd:choice>
...

&如果我要从 xml 中删除 subElemA 或 subElemB,那么 xml 将成功进行架构验证。

update2

我希望找到一个解决方案,其中涉及调整我的模式验证代码以允许模式验证器绕过此检查;模式验证代码:

   XmlReaderSettings setting = new XmlReaderSettings();
   setting.ConformanceLevel = ConformanceLevel.Document;
   setting.IgnoreComments = true;
   setting.IgnoreWhitespace = true;
   setting.IgnoreProcessingInstructions = true;
   setting.ValidationType = ValidationType.Schema;
   setting.Schemas.Add(defaultNameSpace, schemaLocation.AbsoluteUri);
   setting.ValidationEventHandler += new ValidationEventHandler(this.SchemaValidationCallBack);
   MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xmlToValidate));
   XmlReader reader = XmlReader.Create(ms, setting);
   while (reader.Read());

I have an xml query which should be xsd validated ...(impossible ??)

Unfortunately the xsd schema for the xml contains an xsd:choice between two sequences.

but i am specifying both these sequences in my xml query.

this is causing the xsd to fail the xml, any direction as to how i override this check ?

Provided:

1. The XSD schema is standardized & cannot be altered.

2. Any change must be done at the C# code behind where i am applying the validation.

the same has been asked in :

Ignore element order while validating XML against XSD

but the answer there was to modify the xsd schema, which was the accepted answer by the user.

update1

the xml is similar to this :

...
<elem>
    <subElemA>textValA</subElemA>
    <subElemB>textValA</subElemB>
<elem>
...

but the XSD says :

...
<xsd:choice>
    <xsd:element name="subElemA" minOccurs="0" maxOccurs="1">
    </xsd:element>
    <xsd:element name="subElemB" minOccurs="0" maxOccurs="1">
    </xsd:element>
</xsd:choice>
...

& if i were to remove either subElemA OR subElemB from the xml, then the xml will schema validate successfully.

update2

i am hoping to find a solution which involves tweaking my schema validation code to allow the schema validator to bypass this check; Code for schema validation :

   XmlReaderSettings setting = new XmlReaderSettings();
   setting.ConformanceLevel = ConformanceLevel.Document;
   setting.IgnoreComments = true;
   setting.IgnoreWhitespace = true;
   setting.IgnoreProcessingInstructions = true;
   setting.ValidationType = ValidationType.Schema;
   setting.Schemas.Add(defaultNameSpace, schemaLocation.AbsoluteUri);
   setting.ValidationEventHandler += new ValidationEventHandler(this.SchemaValidationCallBack);
   MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xmlToValidate));
   XmlReader reader = XmlReader.Create(ms, setting);
   while (reader.Read());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2025-01-08 04:21:01

您尝试做的事情很可能是不可能的。从验证者的角度来看,XML 模式是您尝试验证的契约。如果您的文档与该合同不一致,那么验证器就无法接受它(否则它将违背您的要求)。

如果您确实需要接受此 XML 文档,则指示验证器接受它的方法是更改​​其尝试执行的契约(模式)。实现此目标的最佳方法是与第三方合作,帮助他们解决架构与所提供的实例文档之间的冲突。如果不这样做,任何其他试图履行合同的人都会遇到与您相同的问题。

如果这是不可能的,您可以通过创建一个本地架构文件来绕过系统,该文件已被修改以允许验证实例文档。不要将客户架构加载到验证器中,而是加载替代实现。这将允许您验证文档,而无需第三方更新官方架构。这很容易变成维护问题,因为您的本地副本可能与原始架构中的更改不同步,但是如果他们不愿意更改此实例文档的架构,则架构可能不会更改无论如何经常。

It's likely that what you're attempting to do isn't possible. From the validator's perspective, the XML schema is the contract that you're attempting to verify. If your document is inconsistent with that contract, then the validator can't accept it (otherwise it would be going against what you've asked it to do).

If you really need to accept this XML document, the way you would instruct the validator to accept it would be to change the contract its trying to enforce (the schema). The best way to accomplish this would be to work with your third party to help them resolve conflicts between their schema and the instance documents they're providing. Without doing this, anyone else attempting to fulfill the contract is going to run into the same problems you are.

If that's not possible, you could circumvent the system by creating a local schema file that has been modified to allow the instance document to be validated. Instead of loading the customers schema into your validator, load your alternate implementation instead. This would allow you to validate the document without the third party updating the official schema. This can easily turn into a maintenance issue as your local copy may become out-of-sync with changes in the original schema, however if they're unwilling to change the schema for this instance document, it is possible the schema isn't changing frequently anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文