没有架构的令人厌恶的 XML 配置 - 为什么?
这不是关于为什么 XML 用于配置文件的问题。我的问题是为什么,当 XML 用于配置文件时,没有伴随的架构。
log4net 就是一个很好的例子。您可以通过编程方式对其进行配置,但最好使用 XML。很好,但是为什么要这么难呢?
在 Visual Studio 中,当我使用架构输入 XML 时,我会得到很好的智能感知。如果没有,我就不得不猜测每一个元素和属性,因为似乎从来没有一个全面的列表。 XML 变成了令人厌恶的 XML,这种 XML 很容易出错并且输入起来很乏味。
从事过此类项目的人:为什么发布它们时带有文档、XML 注释和示例,但没有 XML 模式来使配置更容易?是懒惰吗?其背后是否有真正的动机?难道没人想到吗?
This isn't a question about why XML is used for configuration files. My question is why, when XML is used for configuration files, there's no accompanying schema.
log4net is a good case-in-point. You can configure it programmatically, but it's preferred that you use XML. Fine, but then why does it have to be so hard?
In Visual Studio when I'm typing XML with a schema, I get nice Intellisense as I go. Without one, I'm having to guess at every element and attribute, since there never seems to be a comprehensive listing. The XML becomes Disgusting XML, the kind that's error-prone and tedious to type.
Those of you who've worked on such projects: Why do you release them with documentation and XML comments and samples, but no XML schema to make configuration easier? Is it laziness? Is there a genuine motive behind it? Did no one think of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信,虽然大多数开发人员都非常了解 XML,并且利用这些知识他们可以构建读取 XML 配置文件的程序,但 XML 模式却没有得到很好的理解。
事实上,大多数用于编写 XML 模式的工具要么要花很多钱,要么可以免费获得,但使用起来不太好或不好,这一事实可能会让情况变得更糟。这样,认真试验 XML 模式就会变得更加困难。
(我知道,当然,文本编辑器应该足以编写 XML 和 XSD。但恕我直言,是否有专门的工具通常会产生至关重要的影响。)
I believe that while most developers understand XML quite well, and with that knowledge they can build programs that read XML configuration files, XML schemas aren't nearly as well understood.
This is probably made worse by the fact that most tools for writing XML schemas either cost a good bit of money, or are freely available, but then not very good or nice to work with. It then becomes just a little bit more difficult to seriously experiment with XML schemas.
(I know, of course a text editor should be enough for writing both XML and XSD. But it often makes a crucial difference whether specialized tools are available or not, IMHO.)
我认为主要原因是,在大多数情况下,您已经将您对文档中期望内容的期望放入了评估 xml 的代码中。因此,模式可以被视为额外的工作。即使开源工作也不是免费的......;D
I think the main reason is, that in the most cases you already put your expectation about what you expect in a document in the code that evaluates the xml. Therefore a schema could be seen as extra work. And even in open-source work is not for free... ;D
不,我不会为 XSchema 操心,因为该标准笨重、不灵活,并且不提供除基本类型语法验证之外的任何内容。
因此,它允许我定义文件的语法和值的基本数据类型,但我仍然必须为任何重要参数编写自己的验证例程。即使这样,由于无法定义我自己的词汇形式,或者无法定义我自己的限制或超出基本范围限制的基本 xsd 数据类型的扩展,使得规范无法表达我需要的语法形式,使其真正有用,甚至对于语法验证也是如此。
考虑一下如何声明元素的 text() 应该是有效的 ISBN?或者有效的文件名/目录路径?还是素数?还是闰年? ETC? ETC? ETC?
就我个人而言,如果您幸运的话,我可能会为您提供 DTD(即奇怪格式的非类型化 EBNF)。我什至可能会在其上运行自动 DTD 到 RelaxNG 转换器并为您清理结果。但不要指望我会写一个模式。如果我想提供真正的验证支持,无论如何您都不会获得 XSchema;相反,我将向您提供 GRDDL+OWL,其中在 XSLT 和声明性 FOL 约束语言的强大功能之间,我实际上可以声明值得执行的验证。
Nah, I'm not going to bother with an XSchema because the standard is clunky, inflexible, and doesn't provide anything beyond basic typed-syntactic validation.
So it allows me to define a grammar for the file, and basic datatypes for the values, but I'm still going to have to write my own verification routines for any non-trivial parameters. Even then, the inability to define my own lexical forms, or define my own restrictions or extensions of basic xsd datatypes beyond basic range constraints, renders the spec incapable of expressing the syntactic forms I would need to make it truly useful even for syntactic validation.
Just consider how you might declare that the text() of an element should be a valid ISBN? Or a valid filename/directory-path? Or a prime number? Or a leap-year? etc? etc? etc?
Personally I might provide you with a DTD if you're lucky (ie. an untyped EBNF in a weird format). I might even run an automatic DTD to RelaxNG converter over it and clean up the result for you. But don't expect me to write a schema. If I'm wanting to provide true validation support, you won't be getting an XSchema anyway; rather, I'll be handing you GRDDL+OWL, where between XSLT and the power of a declarative FOL constraint language, I can actually declare validations that are worth doing.