如何在 Ruby 中通过 RELAX NG 验证 XML?

发布于 2024-07-23 07:41:35 字数 250 浏览 8 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

凡间太子 2024-07-30 07:41:35

嗯,我构建了一个程序,但结果并不好。

我的结论如下:

  1. rexmlrelaxng模式解析可能不起作用。 代码指出它是不完整的
  2. rexml pull 解析可能有效,但很难说
  3. 以上两者都没有记录,
  4. 您应该使用真正的 XML 库,例如 libxml

这是我的测试程序:test.rb

require 'rexml/validation/relaxng.rb'
require 'rexml/parsers/pullparser.rb'

# USAGE: ruby test.rb XML-FILE
xml = ARGV[0]

# schema must be a Relax NG XML (NOT compact / .rnc)
schema = File.new( "example.rng" )
validator = REXML::Validation::RelaxNG.new( schema )

# The structure the validator made, which should be a complex structure but isn't
validator.dump

xmlfile = File.new( xml )
parser = REXML::Parsers::PullParser.new( xmlfile )
while parser.has_next?
  # Returns an PullEvent
  e = parser.pull
  # puts "Event ", e.inspect
  validator.validate(e)
end

,我做了一些玩具示例 XML 文件和 RNG 文件,然后在 OSX 10.5.x 上尝试
(为了可读而断掉长行):(

$ /usr/bin/ruby test.rb good.xml 
< S.1 #{doc}, :end_document(  ) >
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/
  validation/validation.rb:24:in `validate': Validation error.  Expected:
  :start_element( doc ) from < S.1 #:start_element( doc ), {head}, {body},
  :end_element(  ), :end_document(  ) >  but got "doc"(  )
  (REXML::Validation::ValidationException)
        from test.rb:20

我在 1.9 中得到了同样的结果)

所以,几乎失败了。

(我可以进一步优化测试程序以使用 add_listener 但它似乎不值得)

Well, I got a program constructed but the results aren't good.

My conclusions are as follows:

  1. rexml relaxng schema parsing probably does not work. the code notes it is incomplete
  2. rexml pull parsing probably works but hard to tell
  3. both of the above are undocumented
  4. you should use a real XML library such as libxml

Here's my test program: test.rb

require 'rexml/validation/relaxng.rb'
require 'rexml/parsers/pullparser.rb'

# USAGE: ruby test.rb XML-FILE
xml = ARGV[0]

# schema must be a Relax NG XML (NOT compact / .rnc)
schema = File.new( "example.rng" )
validator = REXML::Validation::RelaxNG.new( schema )

# The structure the validator made, which should be a complex structure but isn't
validator.dump

xmlfile = File.new( xml )
parser = REXML::Parsers::PullParser.new( xmlfile )
while parser.has_next?
  # Returns an PullEvent
  e = parser.pull
  # puts "Event ", e.inspect
  validator.validate(e)
end

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):

$ /usr/bin/ruby test.rb good.xml 
< S.1 #{doc}, :end_document(  ) >
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/
  validation/validation.rb:24:in `validate': Validation error.  Expected:
  :start_element( doc ) from < S.1 #:start_element( doc ), {head}, {body},
  :end_element(  ), :end_document(  ) >  but got "doc"(  )
  (REXML::Validation::ValidationException)
        from test.rb:20

(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)

地狱即天堂 2024-07-30 07:41:35

我已经成功使用 Nokogiri (从 libxml-ruby gem 切换之后,因为它每次使用 v1.1.3 时都会出现段错误,尽管更改日志表明某些 Windows 段错误问题已得到解决)。

这是我正在使用的代码:

首先,安装Nokogiri,查看安装教程 如果您遇到问题。

gem install nokogiri

如果在 Rails 上运行,请在 "Rails.root/config/enviroment.rb" 中配置 gem,例如:

config.gem 'nokogiri'

运行 Ruby,只需 require "nokogiri

相反,如果 根据预定义的 RelaxNG 模式验证 XML 文档(我们假设文件存储在 'public' 中),请使用以下代码片段:

schema_path = "public/mySchema.rng"    # Or any valid path to a .RNG File
doc_path    = "public/myInstance.xml"  # Or any valid path to a .XML File

schema = Nokogiri::XML::RelaxNG(File.open(schema_path))

instance = Nokogiri::XML(File.open(doc_path))
errors = schema.validate(instance)

is_valid = errors.empty?

希望这会有所帮助!

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.

gem install nokogiri

If running on Rails, config the gem in your "Rails.root/config/enviroment.rb", for instance :

config.gem 'nokogiri'

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 :

schema_path = "public/mySchema.rng"    # Or any valid path to a .RNG File
doc_path    = "public/myInstance.xml"  # Or any valid path to a .XML File

schema = Nokogiri::XML::RelaxNG(File.open(schema_path))

instance = Nokogiri::XML(File.open(doc_path))
errors = schema.validate(instance)

is_valid = errors.empty?

Hope this helps !

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