用于检查 XML 配置文件内部一致性的工具、规则或流程
我曾经参与过具有非常复杂的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这在很大程度上取决于您的项目使用的语言/框架/工具。
使用 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.
您尝试过 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
我使用自动化功能测试来确保我们默认配置在工作中的数据完整性。我不知道你描述的问题是否一定依赖于 XML 的配置格式。话虽这么说,我建议:
您可能无法对 XML 作为配置格式、不良耦合或配置中的代码执行执行任何操作。您可以通过创建一套功能测试来降低风险,这些测试确保:
有时您还可以通过提供替代方案(配置或配置条目)来帮助更改。
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 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:
You can also sometimes help change by providing an alternative (config or config entries).
@David Peleg 的一个很好的链接是 Topologi,它提供了检查 XML 的产品,包括“业务规则”检查。
A good link on from @David Peleg's is Topologi, which offer products to check XML, including "business rules" checks.