71"""Convert keyword args to a dictionary of stylesheet parameters. 72 XSL stylesheet parameters must be XPath expressions, i.e.: 73 74 * string expressions, like "'5'" 75 * simple (number) expressions, like "5" 76 * valid XPath expressions, like "/a/b/text()" 77 78 This function converts native Python keyword arguments to stylesheet 79 parameters following these rules: 80 If an arg is a string wrap it with XSLT.strparam(). 81 If an arg is an XPath object use its path string. 82 If arg is None raise TypeError. 83 Else convert arg to string. 84 """ 85result={} 86forkey,valinkwargs.items(): 87ifisinstance(val,basestring): 88val=_etree.XSLT.strparam(val) 89elifvalisNone: 90raiseTypeError('None not allowed as a stylesheet parameter') 91elifnotisinstance(val,_etree.XPath): 92val=unicode(val) 93result[key]=val 94returnresult
95
96 97# helper function for use in Schematron __init__ 98-def_stylesheet_param_dict(paramsDict,kwargsDict):
99"""Return a copy of paramsDict, updated with kwargsDict entries, wrapped as100 stylesheet arguments.101 kwargsDict entries with a value of None are ignored.102 """103# beware of changing mutable default arg104paramsDict=dict(paramsDict)105fork,vinkwargsDict.items():106ifvisnotNone:# None values do not override107paramsDict[k]=v108paramsDict=stylesheet_params(**paramsDict)109returnparamsDict
113"""An ISO Schematron validator.114115 Pass a root Element or an ElementTree to turn it into a validator.116 Alternatively, pass a filename as keyword argument 'file' to parse from117 the file system.118119 Schematron is a less well known, but very powerful schema language.120 The main idea is to use the capabilities of XPath to put restrictions on121 the structure and the content of XML documents.122123 The standard behaviour is to fail on ``failed-assert`` findings only124 (``ASSERTS_ONLY``). To change this, you can either pass a report filter125 function to the ``error_finder`` parameter (e.g. ``ASSERTS_AND_REPORTS``126 or a custom ``XPath`` object), or subclass isoschematron.Schematron for127 complete control of the validation process.128129 Built on the Schematron language 'reference' skeleton pure-xslt130 implementation, the validator is created as an XSLT 1.0 stylesheet using131 these steps:132133 0) (Extract from XML Schema or RelaxNG schema)134 1) Process inclusions135 2) Process abstract patterns136 3) Compile the schematron schema to XSLT137138 The ``include`` and ``expand`` keyword arguments can be used to switch off139 steps 1) and 2).140 To set parameters for steps 1), 2) and 3) hand parameter dictionaries to the141 keyword arguments ``include_params``, ``expand_params`` or142 ``compile_params``.143 For convenience, the compile-step parameter ``phase`` is also exposed as a144 keyword argument ``phase``. This takes precedence if the parameter is also145 given in the parameter dictionary.146147 If ``store_schematron`` is set to True, the (included-and-expanded)148 schematron document tree is stored and available through the ``schematron``149 property.150 If ``store_xslt`` is set to True, the validation XSLT document tree will be151 stored and can be retrieved through the ``validator_xslt`` property.152 With ``store_report`` set to True (default: False), the resulting validation153 report document gets stored and can be accessed as the ``validation_report``154 property.155156 Here is a usage example::157158 >>> from lxml import etree159 >>> from lxml.isoschematron import Schematron160161 >>> schematron = Schematron(etree.XML('''162 ... <schema xmlns="http://purl.oclc.org/dsdl/schematron" >163 ... <pattern id="id_only_attribute">164 ... <title>id is the only permitted attribute name</title>165 ... <rule context="*">166 ... <report test="@*[not(name()='id')]">Attribute167 ... <name path="@*[not(name()='id')]"/> is forbidden<name/>168 ... </report>169 ... </rule>170 ... </pattern>171 ... </schema>'''),172 ... error_finder=Schematron.ASSERTS_AND_REPORTS)173174 >>> xml = etree.XML('''175 ... <AAA name="aaa">176 ... <BBB id="bbb"/>177 ... <CCC color="ccc"/>178 ... </AAA>179 ... ''')180181 >>> schematron.validate(xml)182 False183184 >>> xml = etree.XML('''185 ... <AAA id="aaa">186 ... <BBB id="bbb"/>187 ... <CCC/>188 ... </AAA>189 ... ''')190191 >>> schematron.validate(xml)192 True193 """194195# libxml2 error categorization for validation errors196_domain=_etree.ErrorDomains.SCHEMATRONV197_level=_etree.ErrorLevels.ERROR198_error_type=_etree.ErrorTypes.SCHEMATRONV_ASSERT199200# convenience definitions for common behaviours201ASSERTS_ONLY=svrl_validation_errors# Default202ASSERTS_AND_REPORTS=_etree.XPath(203'//svrl:failed-assert | //svrl:successful-report',204namespaces={'svrl':SVRL_NS})205
324"""ISO-schematron skeleton implementation XSLT validator document (None325 if object has been initialized with store_xslt=False).326 """327returnself._validator_xslt