2017-10-16 3 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 
関連する問題