如何在 Ruby 中通过 RELAX NG 验证 XML?
REXML
模块似乎支持 RELAX NG 验证,但文档没有任何有关使用框架验证部分的真实信息。
您将如何使用 RELAX NG 模式验证 XML 文档? 代码片段将是最有帮助的。 蒂亚!
The REXML
module appears to have support for RELAX NG validation, but the docs don't have any real information on using the validation portion of the framework.
How would you validate an XML document with a RELAX NG schema? A code snippet would be most helpful. TIA!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我构建了一个程序,但结果并不好。
我的结论如下:
这是我的测试程序:test.rb
,我做了一些玩具示例 XML 文件和 RNG 文件,然后在 OSX 10.5.x 上尝试
(为了可读而断掉长行):(
我在 1.9 中得到了同样的结果)
所以,几乎失败了。
(我可以进一步优化测试程序以使用 add_listener 但它似乎不值得)
Well, I got a program constructed but the results aren't good.
My conclusions are as follows:
Here's my test program: test.rb
and I made some toy example XML files and RNG files and then tried it out on OSX 10.5.x
(long line broken to make it readable):
(I get the same with 1.9)
So, pretty much failure.
(I could have optimized the test program some more to use add_listener but it didn't seem worthwhile)
我已经成功使用 Nokogiri (从
libxml-ruby
gem 切换之后,因为它每次使用 v1.1.3 时都会出现段错误,尽管更改日志表明某些 Windows 段错误问题已得到解决)。这是我正在使用的代码:
首先,安装Nokogiri,查看安装教程 如果您遇到问题。
如果在 Rails 上运行,请在
"Rails.root/config/enviroment.rb
" 中配置 gem,例如:运行 Ruby,只需
require "nokogiri
。相反,如果 根据预定义的 RelaxNG 模式验证 XML 文档(我们假设文件存储在
'public'
中),请使用以下代码片段:希望这会有所帮助!
I've had success with Nokogiri (after switching from the
libxml-ruby
gem, since it segfault'ed every time with v1.1.3, although the changelog says that some Windows segfault issues have been resolved).Here's the code I'm using :
First off, install Nokogiri, take a look at the installation tutorial if you're having issues.
If running on Rails, config the gem in your
"Rails.root/config/enviroment.rb
", for instance :Conversely, just
require "nokogiri
if running Ruby.To validate an XML document based on a pre-defined RelaxNG schema (we'll assume the files are stored in
'public'
), use this snippet :Hope this helps !