用于检查 XML 配置文件内部一致性的工具、规则或流程

发布于 2024-10-06 15:10:50 字数 487 浏览 5 评论 0原文

我曾经参与过具有非常复杂的 XML 配置的项目,面临的一个问题是维护 XML 的内部一致性。

在这种情况下,我并不是指严格的 XML 模式一致性,而是指所使用的节点之间的更高级别的关系。大多数问题是由 XML 中编码的信息之间的隐式链接以及该信息与代码库的隐式关系引起的。示例可能是:

  • XML 节点数据隐式链接到代码中的枚举
  • 相关配置中的业务对象(因为它们共享需要一致的信息),它们之间没有任何显式关系
  • XML 中的代码要在运行时编译和解析

让我震惊的是,a)这可能成为增加频率的一种做法,b)在某些情况下,我们隐式地创建了一种不进行编译时检查的新编码语言——事实上,在运行之前几乎没有检查。

还有其他人面临类似的情况吗?是否有任何工具或方法可以使问题更容易处理?我想要一些与技术无关的一般示例——我自己的具体经验是使用 C# 和专有系统的配置。

注意:虽然我在下面对此有一个答案,但我无意将自己的答案作为最终答案。

I've worked on projects that have very complex XML configuration, and one problem that's faced is maintaining the internal consistency of the XML.

In this case I don't mean the strict XML schema consistency, but rather the higher level relation between the nodes used. Most problems were caused by implicit linking between information encoded in the XML, and implicit relation of that information to the codebase. Examples might be:

  • XML node data implicitly linked to enums in code
  • Business objects in the same config that are related (in that they share information that needs to be consistent) without any explicit relation between them
  • Code in XML to be compiled and parsed at runtime

It struck me that a) this might become a practice of increasing frequency and b) that in some cases we're implicitly creating a new coding language that's not compile-time checked -- and in fact has few checks at all until it's run.

Is anyone else out there facing similar scenarios, and are there any tools or approaches that make the problem more tractable? I'd like some general examples that are technology-agnostic -- my own specific experience has been with C# and with config for proprietary systems.

Note: although I have an answer to this below, I've no intent of taking my own as the final answer.

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

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

发布评论

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

评论(4

逆光飞翔i 2024-10-13 15:10:50

这在很大程度上取决于您的项目使用的语言/框架/工具。

使用 XML 进行配置可能确实存在问题,因为无法在编译时检查它。

例如,当使用 Java 和 Spring Framework 时,存在一个名为 Spring Tool Suite 可以在XML配置和实际代码之间进行同步检查。

但这只是特定语言和特定框架的一个示例。您应该尝试找出您的场景是否存在类似的情况。

顺便说一句,如果您让我们知道您正在使用哪些技术,我们也许可以为您提供更多帮助。

This will greatly depend on languages/frameworks/tools you're using for your project.

Using XML for configuration can be really problematic because it cannot be compile time checked.

For example, when using Java and Spring Framework, there exists an Eclipse plugin called Spring Tool Suite which can do synchronization checking between XML configuration and the actual code.

But this is just one example for a specific language and a specific framework. You should try to find out if something similiar exists for your scenario.

By the way, if you let us know what technologies you're using, we might be able to assist you more.

东走西顾 2024-10-13 15:10:50

您尝试过 Schematron 吗?
http://www.schematron.com/

它是一种高级语言,旨在从语义上验证 XML,而不仅仅是在语法上。

另请参阅维基百科:http://en.wikipedia.org/wiki/Schematron

Did you try Schematron?
http://www.schematron.com/

It's a higher level language targeted at validating XML semantically, not just syntactically.

See also Wikipedia: http://en.wikipedia.org/wiki/Schematron

初与友歌 2024-10-13 15:10:50

我使用自动化功能测试来确保我们默认配置在工作中的数据完整性。我不知道你描述的问题是否一定依赖于 XML 的配置格式。话虽这么说,我建议:

  • 不要使用 XML 作为配置格式。 (谷歌可以给你原因)
  • 通过隐式使用枚举或任何其他构造值的紧密耦合在配置中与在代码中一样糟糕。
  • 从配置中提取并执行的代码让我感到畏缩。它是否在配置中以便可以被覆盖?如果是这样,那就是错误、未定义行为和潜在安全问题的巨大载体。
  • 业务对象问题听起来也像是不好的紧密耦合。

您可能无法对 XML 作为配置格式、不良耦合或配置中的代码执行执行任何操作。您可以通过创建一套功能测试来降低风险,这些测试确保:

  • 枚举有效、代码
  • 可编译、可执行、不包含 eval、rpc 调用、不调用函数或您想要添加的任何其他分析检查。

有时您还可以通过提供替代方案(配置或配置条目)来帮助更改。

I use automated functional tests to ensure the data integrity of our default config at work. I don't know that the problem you describe is necessarily dependent on the config format being XML. That being said I would suggest:

  • You don't use XML as a config format. (Google can give you the reasons)
  • Tight coupling through the implicit use of enums or any other constructed values are just as bad in a config as they are in code.
  • Code that get's pulled from a config and executed makes me cringe. Is it in a config so that it can be overridden? If so that's a huge vector for bugs, undefined behavior and potential security issues.
  • the business objects issue also sounds like bad tight coupling.

You may not be able to do anything about XML as the config format, the bad coupling, or the execution of code from config. You can mitigate the risk by creating a suite of functional tests that ensure:

  • the enums are valid
  • the code is compilable, executable, doesn't contain evals, rpc calls, calls no functions, or whatever other analysis checks you want to add.

You can also sometimes help change by providing an alternative (config or config entries).

囚我心虐我身 2024-10-13 15:10:50

@David Peleg 的一个很好的链接是 Topologi,它提供了检查 XML 的产品,包括“业务规则”检查。

A good link on from @David Peleg's is Topologi, which offer products to check XML, including "business rules" checks.

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