ruby – 验证XML:验证根目录没有可用的匹配全局声明

前端之家收集整理的这篇文章主要介绍了ruby – 验证XML:验证根目录没有可用的匹配全局声明前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 Ruby针对XSD架构验证以下XML.
它根本不起作用,停止并显示错误消息

Error: Element ‘request’: No matching global declaration available for the validation root.

也许它是命名空间?有任何想法吗?

XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <request type="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. <channel name="channel">
  4. <username>user</username>
  5. <password>pass</password>
  6. </channel>
  7.  
  8. <hotel id="1">
  9. <date from="2009-07-07" to="2009-07-17"/>
  10. <room id="1">
  11. <allocation>10</allocation>
  12. </room>
  13. </hotel>
  14. </request>

XSD

  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2.  
  3. <!-- channel -->
  4. <xsd:element name="channel">
  5. <xsd:attribute name="name" use="required" type="xsd:string" />
  6. <xsd:sequence>
  7. <xsd:element username="name" use="required" type="xsd:string"/>
  8. <xsd:element password="country" use="required" type="xsd:string"/>
  9. </xsd:sequence>
  10. </xsd:element>
  11.  
  12. <!-- hotel -->
  13. <xsd:element name="hotel">
  14. <xsd:attribute name="id" use="required" type="xsd:string" />
  15. <xsd:sequence>
  16. <xsd:element name="hotel">
  17. <xsd:attribute name="from" use="required" type="xsd:string" />
  18. <xsd:attribute name="to" use="required" type="xsd:string" />
  19. </xsd:element>
  20. <xsd:element ref="room" minOccurs="1"/>
  21. </xsd:sequence>
  22. </xsd:element>
  23.  
  24.  
  25. <!-- room -->
  26. <xsd:element name="room">
  27. <xsd:sequence>
  28. <xsd:element name="allocation" type="xsd:string"></xsd:element>
  29. <xsd:element ref="hotel" minOccurs="1"/>
  30. </xsd:sequence>
  31. <xsd:attribute name="id" use="required" type="xsd:string" />
  32. </xsd:element>
  33.  
  34. <!-- building all together -->
  35. <xsd:element name="request">
  36. <xsd:attribute name="type" use="required" type="xsd:string" />
  37. <xsd:complexType>
  38. <xsd:sequence>
  39. <xsd:element ref="channel" maxOccurs="1"/>
  40. <xsd:element ref="hotel" maxOccurs="1"/>
  41. </xsd:sequence>
  42. </xsd:complexType>
  43. </xsd:element>
  44. </xsd:schema>

Ruby代码

  1. require "xml"
  2.  
  3. document = LibXML::XML::Document.file("/tmp/test.xml")
  4. schema = LibXML::XML::Document.file("/tmp/request.xsd")
  5.  
  6. result = document.validate_schema(schema) do |message,flag|
  7. log.debug(message)
  8. puts message
  9. end

解决方法

这是一个神秘的错误,但可能是因为你的XSD格式不正确.例如,渠道,酒店(内部和外部元素),房间和请求xsd:element标签内容都应包含在xsd:complexType标签中.此外,use仅对xsd:attribute有效,而不对xsd:element有效.对于元素,使用minOccurs和maxOccurs(尽管两者都默认为1,因此在这种情况下它们实际上不是必需的).此外,您的外部酒店元素包含一个房间元素,必须包含一个酒店元素,创建一个无限循环.此外,您没有正确命名您的用户名和密码元素.最后,内部酒店元素应该是约会.以下是我认为您正在寻找的内容
  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2.  
  3. <!-- channel -->
  4. <xsd:element name="channel">
  5. <xsd:complexType>
  6. <xsd:sequence>
  7. <xsd:element name="username" type="xsd:string"/>
  8. <xsd:element name="password" type="xsd:string"/>
  9. </xsd:sequence>
  10. <xsd:attribute name="name" use="required" type="xsd:string" />
  11. </xsd:complexType>
  12. </xsd:element>
  13.  
  14. <!-- hotel -->
  15. <xsd:element name="hotel">
  16. <xsd:complexType>
  17. <xsd:sequence>
  18. <xsd:element name="date">
  19. <xsd:complexType>
  20. <xsd:attribute name="from" use="required" type="xsd:string" />
  21. <xsd:attribute name="to" use="required" type="xsd:string" />
  22. </xsd:complexType>
  23. </xsd:element>
  24. <xsd:element ref="room" minOccurs="1"/>
  25. </xsd:sequence>
  26. <xsd:attribute name="id" use="required" type="xsd:string" />
  27. </xsd:complexType>
  28. </xsd:element>
  29.  
  30.  
  31. <!-- room -->
  32. <xsd:element name="room">
  33. <xsd:complexType>
  34. <xsd:sequence>
  35. <xsd:element name="allocation" type="xsd:string"></xsd:element>
  36. </xsd:sequence>
  37. <xsd:attribute name="id" use="required" type="xsd:string" />
  38. </xsd:complexType>
  39. </xsd:element>
  40.  
  41. <!-- building all together -->
  42. <xsd:element name="request">
  43. <xsd:complexType>
  44. <xsd:sequence>
  45. <xsd:element ref="channel" maxOccurs="1"/>
  46. <xsd:element ref="hotel" maxOccurs="1"/>
  47. </xsd:sequence>
  48. <xsd:attribute name="type" use="required" type="xsd:string" />
  49. </xsd:complexType>
  50. </xsd:element>
  51. </xsd:schema>

猜你在找的Ruby相关文章