2016-06-17 28 views
0

我想在grails中驗證xml文檔對抗xsd 1.1的效果。Grails根據xsd驗證xml文檔1.1

我對驗證碼:

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 

對於一些轉變,我已經使用撒克遜-HE 9.7.0-5

所以什麼都可以我確實讓我的應用程序在XSD 1.1上驗證了嗎?

回答

0

如果你想使用撒克遜作爲架構驗證引擎:

(A)首先確保您加載架構處理器是撒克遜-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")上工廠(撒克遜實現,這首先使得1.1的支持,則返回true)

(B3)調用factory.setProperty( 「http://saxon.sf.net/feature/xsd-version」, 「1.1」)