2017-10-16 9 views
0

나는 Schematron을 사용하여 문서의 유효성을 검사하려고합니다.SchematronParseError : 스키마 트론 스키마가 잘못되었습니다 (ISOSTS 스키마 용)

나는 schema for ISOSTS standard을 사용합니다.

from lxml import etree 
from lxml.isoschematron import Schematron 


def validate(self, filename: str): 
    file = open(filename) 

    schema_filename = join('/path/to/ISOSTS_validation.sch') 
    schema_file = open(schema_filename) 

    # fixme it works. But fails with ISOSTS scheme 
    # schema_file = StringIO('''\ 
    #  <schema xmlns="http://purl.oclc.org/dsdl/schematron" > 
    #  <pattern id="sum_equals_100_percent"> 
    #   <title>Sum equals 100%.</title> 
    #   <rule context="Total"> 
    #   <assert test="sum(//Percent)=100">Sum is not 100%.</assert> 
    #   </rule> 
    #  </pattern> 
    #  </schema> 
    # ''') 

    sct_doc = etree.parse(schema_file) 
    schematron = Schematron(sct_doc)  ## <- FAIL !!! 

    doc = etree.parse(file) 
    result = schematron.validate(doc) 

    file.close() 
    schema_file.close() 

    return result 

validate('/path/to/feature_doc.xml') 

오류 메시지 :

File "/var/www/.../venv/lib/python3.5/site-packages/lxml/isoschematron/__init__.py", line 279, in __init__ 
    schematron_schema_valid.error_log) 
lxml.etree.SchematronParseError: invalid schematron schema: <string>:553:0:ERROR:RELAXNGV:RELAXNG_ERR_EXTRACONTENT: Element function has extra content: param 
<string>:560:0:ERROR:RELAXNGV:RELAXNG_ERR_ELEMNAME: Expecting element schema, got variable 
<string>:0:0:ERROR:RELAXNGV:RELAXNG_ERR_INTEREXTRA: Extra element function in interleave 
<string>:42:0:ERROR:RELAXNGV:RELAXNG_ERR_CONTENTVALID: Element schema failed to validate content 

가 어떻게 수리를?

답변

1

끔찍한 도움이 될지 모르지만 문제가 귀하의 코드에 있다고 생각하지 않습니다. 문제는 lxml이 XSLT-2를 지원하지 않는다는 것입니다.

사용 된 스키마에는 2010 XSLT-2 규격 ISO Schematron [1]이 필요합니다.

Oxygen에서 스키마를 열고 querybinding=xslt2 특성을 제거하면 많은 문제가 발생합니다. 여기에는 553 행 (<xsl:param name="num-cols" as="xs:integer"/>)의 유효성 검사 오류가 포함됩니다. '이 요소에는 특성이 허용되지 않습니다.' 이것은 lxml이 [2]에서 구문 분석 오류를 던지는 줄입니다.

lxml은 XSTL-2를 구현하지 않으며 명시 적으로 Schematron의 "the pure-XSLT-1.0 skeleton implementation"만 지원함을 명시합니다 (정보는 http://lxml.de/validation.html#id2).

lxml을 사용하면이 문제를 해결할 수 있습니다. 필자가 알기에 XSLT-2와 호환되는 Python XML 파서는 없습니다 (누군가가 알고 있다면 환상적입니다).

해킹이 약간 있지만 외부 프로세스 (예 : crux + libsaxon)를 사용하여 유효성 검사를 수행 할 수 있습니다. 이것이 유일한 해결책 일 수 있습니다. 링크 스키마

[1] 라인 35 : <schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"

[2] lxml.etree.SchematronParseError: invalid schematron schema: <string>:553:0:ERROR:RELAXNGV:RELAXNG_ERR_EXTRACONTENT: Element function has extra content: param

0

은 XSD 스키마 from archivelxml.etree.XMLSchema하여 해결 :

def validate(self, filename: str): 
    file = open(filename) 

    schema_filename = '/path/to/ISOSTS.xsd' 
    schema_file = open(schema_filename) 

    sct_doc = etree.parse(schema_file) 
    xmlschema = etree.XMLSchema(sct_doc) 

    doc = etree.parse(file) 
    result = xmlschema.validate(doc) 

    file.close() 
    schema_file.close() 

    return result