且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用XML DTD验证YAML

更新时间:2023-11-06 12:29:22

我不知道这种工具是否存在,但是即使存在,也可能无法足够精确地产生有意义的错误消息给用户.即使编写XML,DTD或XSD也会经常产生非常难以理解的错误消息.

I don't know if such a tool exists, but even if it did, it would probably be impossible for it to be precise enough to yield meaningful error messages to the user. Even when authoring XML, a DTD or XSD will often yield very hard to understand error messages.

但是,编写这样的工具并不难.这就是将YAML转换为可以由XSD中的规则验证的内容.可以通过将YAML转换为XML,然后验证该XML来实现,但是也可以通过将XSD或DTD编译为C#,然后构建C#代码期望的对象树来实现.

However, writing such a tool won't be too hard. It's all about translating the YAML into something that can be validated by the rules in the XSD. That can be achieved by translating YAML to XML and then validating that XML, but it can also be done by compiling an XSD or DTD to C# and then building the object tree expected by the C# code.

也许,如果这样的工具保留了有关YAML的元数据(如行号和字符号),则可能会向用户返回一些有意义的错误消息.如果首先将XSD或DTD转换为C#,则可能尤其如此,因为在C#中,您对XML本身的依赖性较小,并且可以更轻松地将验证扩展为适合YAML的内容.

Perhaps if such a tool preserved metadata about the YAML (like line and character numbers), somewhat meaningful error messages could be returned to the user. That might especially be true if the XSD or DTD is first translated to C#, since in C# you're less dependent on the XML itself and can more easily expand the validation to something that fits with the YAML.