在 C# 中使用 Schematron/XSD 进行 XML 验证

发布于 2024-07-18 03:50:55 字数 211 浏览 3 评论 0原文

我一直在寻找一种对 XML 文档进行更高级业务规则验证的方法。 到目前为止,Schematron 似乎能够满足我的需要。

我环顾四周,发现的几个库都很旧,似乎不再受支持。 Schematron 仅在 2002 年才“火爆”吗? 是否有任何库可以在 C# 中进行验证 - 或者我该如何做?

或者是否有其他可以进行高级验证并拥有 C# 库且已标准化的工具?

I've been looking around for at a way to make some more advanced business rule validation of XML documents. So far Schematron seems to be able to do, what I need.

I've been looking around, and the few libraries I've found was very old and seemingly not supported any longer. Was Schematron only "hot" in 2002?
Are there any libraries that can do the validation in C# - or how do I do it?

Or are there others that can do the advanced validation and have C# libraries and are standardized?

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

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

发布评论

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

评论(1

眉黛浅 2024-07-25 03:50:55

无论如何,这是我的经验......

我曾经有过这样的时刻,我希望模式可以在业务规则验证方面做更多的事情。 然而,根据我的经验,模式是用来定义数据契约(格式、类型等)的......不是来强制执行业务规则。 试图让它超出这个范围会遇到各种各样的问题,这可能就是为什么 Schematron 的受欢迎程度似乎已经消失的原因(可能是我无法备份的糟糕假设)。

为了解决这个问题,通常可以采用两遍方法:

  • 使用模式验证来验证 xml 结构/数据类型等。
  • 应用针对 xml 检查的业务规则(在正常的 C# 代码中)。

我理解,如果您想要采用一种一体化的方法进行验证,但从长远来看,尝试使用架构会导致更多麻烦。 最后,您将通过隔离代码来更好地控制业务规则检查。

For what it's worth, this has been my experience...

I've had moments where I wish schemas could do more along the lines of business rule validation. However, in my experience a schema is there to define a data contract (format, types, etc)... NOT to enforce business rules. Trying to make it more than that runs into all sorts of problems and may be why Schematron seems to have died down in its popularity (possibly bad assumption that I cannot backup).

To get around this, one can generally do a two pass approach:

  • Validate the xml structure/data types, etc using schema validation.
  • Apply business rules checking against the xml (in normal c# code).

I understand if you're wanting to do an all-in-one approach to validation but attempting to use schema for this causes more headaches in the long run. In the end you'll end up having finer control over your business rule checking by isolating to code.

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