【问题标题】:Grails validate xml document against xsd 1.1Grails 根据 xsd 1.1 验证 xml 文档
【发布时间】:2016-10-19 10:42:01
【问题描述】:

我想在 grails 中针对 xsd 1.1 验证 xml 文档。

我的验证代码:

def checkXmlAgainstXsd(InputStream xsd, InputStream xml) throws IOException, SAXException {
    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(xsd))
    def validator = schema.newValidator()
    validator.validate(new StreamSource(xml))
}

如何针对 xsd 1.1 进行验证?

当我尝试这个 xsd 时:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="WaitForSoap">
  <xs:complexType mixed="true">
    <xs:all>
       <xs:element name="Firstname" maxOccurs="3">
         <xs:simpleType>
           <xs:restriction base="xs:string"></xs:restriction>
         </xs:simpleType>
       </xs:element>
       <xs:element name="Lastname" minOccurs="1">
         <xs:simpleType>
           <xs:restriction base="xs:string"></xs:restriction>
          </xs:simpleType>
        </xs:element>
    </xs:all>
  </xs:complexType>
 </xs:element>
</xs:schema>

我得到错误:

org.xml.sax.SAXParseException; lineNumber: 5; 
columnNumber: 9; cos-all-limited.2: The {max occurs} of an 
element in an 'all' model group must be 0 or 1

对于某些转换,我已经使用 Saxon-HE 9.7.0-5

那么我可以做些什么来让我的应用程序针对 XSD 1.1 进行验证?

【问题讨论】:

    标签: validation grails xsd saxon


    【解决方案1】:

    如果您想使用 Saxon 作为架构验证引擎:

    (A) 首先确保您正在加载的架构处理器是 Saxon-EE,并且您已安装许可证密钥。

    (B) 然后确保启用了对 XSD 1.1 的支持。

    对于 (A),确保这一点的最简单方法是直接实例化 Saxon-EE。我不知道 Grails,所以我不能给你代码,但你想用“new com.saxonica.ee.jaxp.SchemaFactoryImpl”替换“SchemaFactory.newInstance(...)”。或者,您可以使用各种 JAXP 机制来选择要加载的工厂类。

    对于(B)你可以:

    (B1) 将字符串“http://www.w3.org/XML/XMLSchema/v1.1”作为您想要的架构语言的标识符传递给 newInstance() 方法

    (B2) 在工厂调用 isSchemaLanguageSupported("http://www.w3.org/XML/XMLSchema/v1.1")(在 Saxon 实现中,这首先启用对 1.1 的支持,然后返回 true)

    (B3) 调用 factory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1")

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      相关资源
      最近更新 更多