Grails 根据 xsd 1.1 验证 xml 文档
Grails validate xml document against xsd 1.1
我想根据 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 进行验证?
如果您想使用 Saxon 作为架构验证引擎:
(A) 首先确保您加载的模式处理器是 Saxon-EE,并且您安装了许可证密钥。
(B) 然后确保启用对 XSD 1.1 的支持。
对于 (A),确保这一点的最简单方法是直接实例化 Saxon-EE。我不知道 Grails,所以我不能给你代码,但你想用 "new com.saxonica.ee.jaxp.SchemaFactoryImpl" 替换 "SchemaFactory.newInstance(...)"。或者,您可以使用各种 JAXP 机制来选择要加载的工厂 class。
对于 (B) 你可以:
(B1) 将字符串“http://www.w3.org/XML/XMLSchema/v1.1”作为您想要的模式语言的标识符传递给 newInstance() 方法
(B2) 在工厂上调用 isSchemaLanguageSupported("http://www.w3.org/XML/XMLSchema/v1.1")
(在 Saxon 实现中,这首先启用对 1.1 的支持,然后 returns true)
(B3) 调用 factory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1")
我想根据 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 进行验证?
如果您想使用 Saxon 作为架构验证引擎:
(A) 首先确保您加载的模式处理器是 Saxon-EE,并且您安装了许可证密钥。
(B) 然后确保启用对 XSD 1.1 的支持。
对于 (A),确保这一点的最简单方法是直接实例化 Saxon-EE。我不知道 Grails,所以我不能给你代码,但你想用 "new com.saxonica.ee.jaxp.SchemaFactoryImpl" 替换 "SchemaFactory.newInstance(...)"。或者,您可以使用各种 JAXP 机制来选择要加载的工厂 class。
对于 (B) 你可以:
(B1) 将字符串“http://www.w3.org/XML/XMLSchema/v1.1”作为您想要的模式语言的标识符传递给 newInstance() 方法
(B2) 在工厂上调用 isSchemaLanguageSupported("http://www.w3.org/XML/XMLSchema/v1.1")
(在 Saxon 实现中,这首先启用对 1.1 的支持,然后 returns true)
(B3) 调用 factory.setProperty("http://saxon.sf.net/feature/xsd-version", "1.1")