启用片段级一致性时会忽略 XML 模式验证吗?

发布于 2024-09-18 05:51:37 字数 940 浏览 7 评论 0原文

从我对 XML 和架构验证的观察来看,似乎当片段级一致性< /a> 已为 XMLReader 实例启用, XML 源停止根据任何包含的架构进行验证。

但是,我无法从 MSDN 文档中验证这一点,因为它似乎没有根据一致性级别描述 XSD 架构行为。另外,如果我认为这个问题是真的,我找不到解决方法。

我想知道如何根据 XSD 架构验证 XML片段

在我的例子中,我正在根据 XHTML 1 严格架构。事实上,像

这样的 HTML 标签已被弃用。没有被标记为无效,这是我相信片段一致性忽略模式的部分原因。此外,当我在同一场景中使用文档级一致性时,有效性错误会被成功标记。

有关我正在使用的验证方案类型的代码示例,请参阅此。

From my sojourn with XML and Schema validation, it seems that when fragment-level conformance is enabled for an XMLReader instance, the XML source stops being validated against any included Schemas.

However I cannot verify this from the MSDN documentation as it doesn't seem to describe XSD Schema behaviour in light of conformance level. Also if I assume this problem is true, I cannot find a workaround for it.

I would like to know how to validate a XML fragment against an XSD Schema.

In my case I'm validating against the XHTML 1 Strict Schema. By the fact deprecated HTML tags like <center> are not being flagged as invalid, this is part of the reason I believe fragment conformance ignores schema. Also when I use document-level conformance in the same scenario, validity errors are successfully flagged.

For a code sample of the type of validation scenario I'm using see this.

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

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

发布评论

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

评论(1

不爱素颜 2024-09-25 05:51:37

我想我在这篇MSKB文章中找到了答案,标题很奇怪如何:验证Visual C#.NET 中针对 XML 架构的 XML 片段 文章底部指出:

注意:只有类型声明和
XML 中的顶级元素
模式根据 XML 进行验证
架构。子元素被视为
本地的,因此不能
已验证。为了验证一个
子元素,声明一个顶级
元素并引用它。

我认为该段落中有一个错误,其目的是说明 XML 中的顶级元素schema 文档/片段 根据 XML 架构进行验证

因为 Microsoft 代码示例在顶层(而不是在文档中任意地)开始其片段,所以它可以进行验证。然而,就我而言,我采用的是中间文档结构。

如果我的解释有误,请纠正我。


解决方法/解决方案

解决方法是我将片段填充到顶级元素中以进行验证。

换句话说,如果我正在处理 XHTML 片段:

<div>MY FRAGMENT</div>

我可以将其包装以进行验证 符合 XHTML 严格架构

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML fragment enclosed, conforming to XHTML 1.0 Strict Template.</title>
</head>
<body>

    <div>MY FRAGMENT</div>

</body>
</html>

此解决方案适合我,因此我将暂时使用它;我还不确定是否存在更好的解决方案。

I think I found the answer in this MSKB article oddly enough titled HOW TO: Validate XML Fragments Against an XML Schema in Visual C#.NET The bottom of the article states:

NOTE: Only the type declarations and
the top-level elements in the XML
schema are validated against an XML
schema. Subelements are considered as
local and therefore cannot be
validated. In order to validate a
subelement, declare a top-level
element and refer to that.

I believe there is a mistake in that paragraph and the intention is to state top-level elements in the XMLschema document/fragment are validated against an XML Schema

Because the Microsoft code sample starts its fragment at the top level (instead of arbitrarily inside a document) it works with validation. However in my case I'm taking a mid-document construct.

Correct me if my interpretation is wrong.


Workaround/solution

A workaround is for me to stuff my fragments inside a top level element for the purpose of validation.

In other words if I'm dealing with the XHTML fragment:

<div>MY FRAGMENT</div>

I can wrap it for validation to conform to the XHTML Strict Schema as:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML fragment enclosed, conforming to XHTML 1.0 Strict Template.</title>
</head>
<body>

    <div>MY FRAGMENT</div>

</body>
</html>

This solution works for me so I'll use it in the interim; whether or not a better solution exists I'm unsure yet.

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