开发者

Validating XML using multiple XSD's in Ruby

I'm generating a lot of XMPP stanzas, and want to validate them against the specs available here in my unit tests.

At the moment I am using Nokogiri to achieve this with something like

xml    = Nokogiri::XML( xmpp_stanza)
schema = Nokogiri::XML::Schema( xmpp_schema )

assert schema.valid?( xml )

Now this works fine except it gets reported as invalid becau开发者_高级运维se each schema only covers one namespace, and my XMPP stanzas have multiple namespaces. For example:

Invalid XML: Element '{http://jabber.org/protocol/pubsub}pubsub': No matching global element declaration available, but demanded by the strict wildcard.

How am I meant to handle multiple schemas to validate a single stanza? Am I meant to first split it apart by namespace and validate each one in isolation?


I was able to achieve this by importing one schema into the other.

e.g.

<xs:import namespace="http://base.google.com/ns/1.0" schemaLocation="public/xsd/google_base.xsd"/>  


If you don't have the other namespaces available, you may also be able to alter the schema to include a processContents="lax" directive on relevant "any" nodes in the schema, saying it's okay NOT to validate namespaces you don't have a schema for. I did like so:

schema_xml        = Nokogiri::XML(File.read(path))
schema_xml.xpath("//xs:any[@namespace='##other']", 
  {"xs" => "http://www.w3.org/2001/XMLSchema"}).each do |node|
     node["processContents"] = "lax"
end   
schema = Nokogiri::XML::Schema.from_document( schema_xml )

Of course, this means that the external namespaces won't be validated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜