/usr/share/doc/python-lxml/html/validation.html is in python-lxml-doc 3.5.0-1ubuntu0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.10: http://docutils.sourceforge.net/" />
<title>Validation with lxml</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="document" id="validation-with-lxml">
<div class="sidemenu"><ul id="lxml-section"><li><span class="section title">lxml</span><ul class="menu foreign" id="index-menu"><li class="menu title"><a href="index.html">lxml</a><ul class="submenu"><li class="menu item"><a href="index.html#introduction">Introduction</a></li><li class="menu item"><a href="index.html#support-the-project">Support the project</a></li><li class="menu item"><a href="index.html#documentation">Documentation</a></li><li class="menu item"><a href="index.html#download">Download</a></li><li class="menu item"><a href="index.html#mailing-list">Mailing list</a></li><li class="menu item"><a href="index.html#bug-tracker">Bug tracker</a></li><li class="menu item"><a href="index.html#license">License</a></li><li class="menu item"><a href="index.html#old-versions">Old Versions</a></li><li class="menu item"><a href="index.html#legal-notice-for-donations">Legal Notice for Donations</a></li></ul></li></ul><ul class="menu foreign" id="intro-menu"><li class="menu title"><a href="intro.html">Why lxml?</a><ul class="submenu"><li class="menu item"><a href="intro.html#motto">Motto</a></li><li class="menu item"><a href="intro.html#aims">Aims</a></li></ul></li></ul><ul class="menu foreign" id="installation-menu"><li class="menu title"><a href="installation.html">Installing lxml</a><ul class="submenu"><li class="menu item"><a href="installation.html#where-to-get-it">Where to get it</a></li><li class="menu item"><a href="installation.html#requirements">Requirements</a></li><li class="menu item"><a href="installation.html#installation">Installation</a></li><li class="menu item"><a href="installation.html#building-lxml-from-dev-sources">Building lxml from dev sources</a></li><li class="menu item"><a href="installation.html#using-lxml-with-python-libxml2">Using lxml with python-libxml2</a></li><li class="menu item"><a href="installation.html#source-builds-on-ms-windows">Source builds on MS Windows</a></li><li class="menu item"><a href="installation.html#source-builds-on-macos-x">Source builds on MacOS-X</a></li></ul></li></ul><ul class="menu foreign" id="performance-menu"><li class="menu title"><a href="performance.html">Benchmarks and Speed</a><ul class="submenu"><li class="menu item"><a href="performance.html#general-notes">General notes</a></li><li class="menu item"><a href="performance.html#how-to-read-the-timings">How to read the timings</a></li><li class="menu item"><a href="performance.html#parsing-and-serialising">Parsing and Serialising</a></li><li class="menu item"><a href="performance.html#the-elementtree-api">The ElementTree API</a></li><li class="menu item"><a href="performance.html#xpath">XPath</a></li><li class="menu item"><a href="performance.html#a-longer-example">A longer example</a></li><li class="menu item"><a href="performance.html#lxml-objectify">lxml.objectify</a></li></ul></li></ul><ul class="menu foreign" id="compatibility-menu"><li class="menu title"><a href="compatibility.html">ElementTree compatibility of lxml.etree</a></li></ul><ul class="menu foreign" id="FAQ-menu"><li class="menu title"><a href="FAQ.html">lxml FAQ - Frequently Asked Questions</a><ul class="submenu"><li class="menu item"><a href="FAQ.html#general-questions">General Questions</a></li><li class="menu item"><a href="FAQ.html#installation">Installation</a></li><li class="menu item"><a href="FAQ.html#contributing">Contributing</a></li><li class="menu item"><a href="FAQ.html#bugs">Bugs</a></li><li class="menu item"><a href="FAQ.html#id1">Threading</a></li><li class="menu item"><a href="FAQ.html#parsing-and-serialisation">Parsing and Serialisation</a></li><li class="menu item"><a href="FAQ.html#xpath-and-document-traversal">XPath and Document Traversal</a></li></ul></li></ul></li></ul><ul id="Developing with lxml-section"><li><span class="section title">Developing with lxml</span><ul class="menu foreign" id="tutorial-menu"><li class="menu title"><a href="tutorial.html">The lxml.etree Tutorial</a><ul class="submenu"><li class="menu item"><a href="tutorial.html#the-element-class">The Element class</a></li><li class="menu item"><a href="tutorial.html#the-elementtree-class">The ElementTree class</a></li><li class="menu item"><a href="tutorial.html#parsing-from-strings-and-files">Parsing from strings and files</a></li><li class="menu item"><a href="tutorial.html#namespaces">Namespaces</a></li><li class="menu item"><a href="tutorial.html#the-e-factory">The E-factory</a></li><li class="menu item"><a href="tutorial.html#elementpath">ElementPath</a></li></ul></li></ul><ul class="menu foreign" id="api index-menu"><li class="menu title"><a href="api/index.html">API reference</a></li></ul><ul class="menu foreign" id="api-menu"><li class="menu title"><a href="api.html">APIs specific to lxml.etree</a><ul class="submenu"><li class="menu item"><a href="api.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="api.html#other-element-apis">Other Element APIs</a></li><li class="menu item"><a href="api.html#trees-and-documents">Trees and Documents</a></li><li class="menu item"><a href="api.html#iteration">Iteration</a></li><li class="menu item"><a href="api.html#error-handling-on-exceptions">Error handling on exceptions</a></li><li class="menu item"><a href="api.html#error-logging">Error logging</a></li><li class="menu item"><a href="api.html#serialisation">Serialisation</a></li><li class="menu item"><a href="api.html#incremental-xml-generation">Incremental XML generation</a></li><li class="menu item"><a href="api.html#cdata">CDATA</a></li><li class="menu item"><a href="api.html#xinclude-and-elementinclude">XInclude and ElementInclude</a></li><li class="menu item"><a href="api.html#write-c14n-on-elementtree">write_c14n on ElementTree</a></li></ul></li></ul><ul class="menu foreign" id="parsing-menu"><li class="menu title"><a href="parsing.html">Parsing XML and HTML with lxml</a><ul class="submenu"><li class="menu item"><a href="parsing.html#parsers">Parsers</a></li><li class="menu item"><a href="parsing.html#the-target-parser-interface">The target parser interface</a></li><li class="menu item"><a href="parsing.html#the-feed-parser-interface">The feed parser interface</a></li><li class="menu item"><a href="parsing.html#incremental-event-parsing">Incremental event parsing</a></li><li class="menu item"><a href="parsing.html#iterparse-and-iterwalk">iterparse and iterwalk</a></li><li class="menu item"><a href="parsing.html#python-unicode-strings">Python unicode strings</a></li></ul></li></ul><ul class="menu current" id="validation-menu"><li class="menu title"><a href="validation.html">Validation with lxml</a><ul class="submenu"><li class="menu item"><a href="validation.html#validation-at-parse-time">Validation at parse time</a></li><li class="menu item"><a href="validation.html#id1">DTD</a></li><li class="menu item"><a href="validation.html#relaxng">RelaxNG</a></li><li class="menu item"><a href="validation.html#xmlschema">XMLSchema</a></li><li class="menu item"><a href="validation.html#id2">Schematron</a></li><li class="menu item"><a href="validation.html#id3">(Pre-ISO-Schematron)</a></li></ul></li></ul><ul class="menu foreign" id="xpathxslt-menu"><li class="menu title"><a href="xpathxslt.html">XPath and XSLT with lxml</a><ul class="submenu"><li class="menu item"><a href="xpathxslt.html#xpath">XPath</a></li><li class="menu item"><a href="xpathxslt.html#xslt">XSLT</a></li></ul></li></ul><ul class="menu foreign" id="objectify-menu"><li class="menu title"><a href="objectify.html">lxml.objectify</a><ul class="submenu"><li class="menu item"><a href="objectify.html#the-lxml-objectify-api">The lxml.objectify API</a></li><li class="menu item"><a href="objectify.html#asserting-a-schema">Asserting a Schema</a></li><li class="menu item"><a href="objectify.html#objectpath">ObjectPath</a></li><li class="menu item"><a href="objectify.html#python-data-types">Python data types</a></li><li class="menu item"><a href="objectify.html#how-data-types-are-matched">How data types are matched</a></li><li class="menu item"><a href="objectify.html#what-is-different-from-lxml-etree">What is different from lxml.etree?</a></li></ul></li></ul><ul class="menu foreign" id="lxmlhtml-menu"><li class="menu title"><a href="lxmlhtml.html">lxml.html</a><ul class="submenu"><li class="menu item"><a href="lxmlhtml.html#parsing-html">Parsing HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-element-methods">HTML Element Methods</a></li><li class="menu item"><a href="lxmlhtml.html#running-html-doctests">Running HTML doctests</a></li><li class="menu item"><a href="lxmlhtml.html#creating-html-with-the-e-factory">Creating HTML with the E-factory</a></li><li class="menu item"><a href="lxmlhtml.html#working-with-links">Working with links</a></li><li class="menu item"><a href="lxmlhtml.html#forms">Forms</a></li><li class="menu item"><a href="lxmlhtml.html#cleaning-up-html">Cleaning up HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-diff">HTML Diff</a></li><li class="menu item"><a href="lxmlhtml.html#examples">Examples</a></li></ul></li></ul><ul class="menu foreign" id="cssselect-menu"><li class="menu title"><a href="cssselect.html">lxml.cssselect</a><ul class="submenu"><li class="menu item"><a href="cssselect.html#the-cssselector-class">The CSSSelector class</a></li><li class="menu item"><a href="cssselect.html#the-cssselect-method">The cssselect method</a></li><li class="menu item"><a href="cssselect.html#supported-selectors">Supported Selectors</a></li><li class="menu item"><a href="cssselect.html#namespaces">Namespaces</a></li></ul></li></ul><ul class="menu foreign" id="elementsoup-menu"><li class="menu title"><a href="elementsoup.html">BeautifulSoup Parser</a><ul class="submenu"><li class="menu item"><a href="elementsoup.html#parsing-with-the-soupparser">Parsing with the soupparser</a></li><li class="menu item"><a href="elementsoup.html#entity-handling">Entity handling</a></li><li class="menu item"><a href="elementsoup.html#using-soupparser-as-a-fallback">Using soupparser as a fallback</a></li><li class="menu item"><a href="elementsoup.html#using-only-the-encoding-detection">Using only the encoding detection</a></li></ul></li></ul><ul class="menu foreign" id="html5parser-menu"><li class="menu title"><a href="html5parser.html">html5lib Parser</a><ul class="submenu"><li class="menu item"><a href="html5parser.html#differences-to-regular-html-parsing">Differences to regular HTML parsing</a></li><li class="menu item"><a href="html5parser.html#function-reference">Function Reference</a></li></ul></li></ul></li></ul><ul id="Extending lxml-section"><li><span class="section title">Extending lxml</span><ul class="menu foreign" id="resolvers-menu"><li class="menu title"><a href="resolvers.html">Document loading and URL resolving</a><ul class="submenu"><li class="menu item"><a href="resolvers.html#xml-catalogs">XML Catalogs</a></li><li class="menu item"><a href="resolvers.html#uri-resolvers">URI Resolvers</a></li><li class="menu item"><a href="resolvers.html#document-loading-in-context">Document loading in context</a></li><li class="menu item"><a href="resolvers.html#i-o-access-control-in-xslt">I/O access control in XSLT</a></li></ul></li></ul><ul class="menu foreign" id="extensions-menu"><li class="menu title"><a href="extensions.html">Python extensions for XPath and XSLT</a><ul class="submenu"><li class="menu item"><a href="extensions.html#xpath-extension-functions">XPath Extension functions</a></li><li class="menu item"><a href="extensions.html#xslt-extension-elements">XSLT extension elements</a></li></ul></li></ul><ul class="menu foreign" id="element classes-menu"><li class="menu title"><a href="element_classes.html">Using custom Element classes in lxml</a><ul class="submenu"><li class="menu item"><a href="element_classes.html#background-on-element-proxies">Background on Element proxies</a></li><li class="menu item"><a href="element_classes.html#element-initialization">Element initialization</a></li><li class="menu item"><a href="element_classes.html#setting-up-a-class-lookup-scheme">Setting up a class lookup scheme</a></li><li class="menu item"><a href="element_classes.html#generating-xml-with-custom-classes">Generating XML with custom classes</a></li><li class="menu item"><a href="element_classes.html#id1">Implementing namespaces</a></li></ul></li></ul><ul class="menu foreign" id="sax-menu"><li class="menu title"><a href="sax.html">Sax support</a><ul class="submenu"><li class="menu item"><a href="sax.html#building-a-tree-from-sax-events">Building a tree from SAX events</a></li><li class="menu item"><a href="sax.html#producing-sax-events-from-an-elementtree-or-element">Producing SAX events from an ElementTree or Element</a></li><li class="menu item"><a href="sax.html#interfacing-with-pulldom-minidom">Interfacing with pulldom/minidom</a></li></ul></li></ul><ul class="menu foreign" id="capi-menu"><li class="menu title"><a href="capi.html">The public C-API of lxml.etree</a><ul class="submenu"><li class="menu item"><a href="capi.html#writing-external-modules-in-cython">Writing external modules in Cython</a></li><li class="menu item"><a href="capi.html#writing-external-modules-in-c">Writing external modules in C</a></li></ul></li></ul></li></ul><ul id="Developing lxml-section"><li><span class="section title">Developing lxml</span><ul class="menu foreign" id="build-menu"><li class="menu title"><a href="build.html">How to build lxml from source</a><ul class="submenu"><li class="menu item"><a href="build.html#cython">Cython</a></li><li class="menu item"><a href="build.html#github-git-and-hg">Github, git and hg</a></li><li class="menu item"><a href="build.html#building-the-sources">Building the sources</a></li><li class="menu item"><a href="build.html#running-the-tests-and-reporting-errors">Running the tests and reporting errors</a></li><li class="menu item"><a href="build.html#building-an-egg-or-wheel">Building an egg or wheel</a></li><li class="menu item"><a href="build.html#building-lxml-on-macos-x">Building lxml on MacOS-X</a></li><li class="menu item"><a href="build.html#static-linking-on-windows">Static linking on Windows</a></li><li class="menu item"><a href="build.html#building-debian-packages-from-svn-sources">Building Debian packages from SVN sources</a></li></ul></li></ul><ul class="menu foreign" id="lxml source howto-menu"><li class="menu title"><a href="lxml-source-howto.html">How to read the source of lxml</a><ul class="submenu"><li class="menu item"><a href="lxml-source-howto.html#what-is-cython">What is Cython?</a></li><li class="menu item"><a href="lxml-source-howto.html#where-to-start">Where to start?</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="lxml-source-howto.html#python-modules">Python modules</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-objectify">lxml.objectify</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-html">lxml.html</a></li></ul></li></ul><ul class="menu foreign" id="changes 3 5 0-menu"><li class="menu title"><a href="changes-3.5.0.html">Release Changelog</a></li></ul><ul class="menu foreign" id="credits-menu"><li class="menu title"><a href="credits.html">Credits</a><ul class="submenu"><li class="menu item"><a href="credits.html#main-contributors">Main contributors</a></li><li class="menu item"><a href="credits.html#special-thanks-goes-to">Special thanks goes to:</a></li></ul></li></ul></li><li><a href="http://lxml.de/sitemap.html">Sitemap</a></li></ul></div><h1 class="title">Validation with lxml</h1>
<p>Apart from the built-in DTD support in parsers, lxml currently supports three
schema languages: <a class="reference external" href="http://en.wikipedia.org/wiki/Document_Type_Definition">DTD</a>, <a class="reference external" href="http://www.relaxng.org/">Relax NG</a> and <a class="reference external" href="http://www.w3.org/XML/Schema">XML Schema</a>. All three provide
identical APIs in lxml, represented by validator classes with the obvious
names.</p>
<p>lxml also provides support for ISO-<a class="reference external" href="http://www.schematron.com">Schematron</a>, based on the pure-XSLT
<a class="reference external" href="http://www.schematron.com/implementation.html">skeleton implementation</a> of Schematron:</p>
<p>There is also basic support for <cite>pre-ISO-Schematron</cite> through the libxml2
Schematron features. However, this does not currently support error reporting
in the validation phase due to insufficiencies in the implementation as of
libxml2 2.6.30.</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#validation-at-parse-time" id="id4">Validation at parse time</a></li>
<li><a class="reference internal" href="#id1" id="id5">DTD</a></li>
<li><a class="reference internal" href="#relaxng" id="id6">RelaxNG</a></li>
<li><a class="reference internal" href="#xmlschema" id="id7">XMLSchema</a></li>
<li><a class="reference internal" href="#id2" id="id8">Schematron</a></li>
<li><a class="reference internal" href="#id3" id="id9">(Pre-ISO-Schematron)</a></li>
</ul>
</div>
<p>The usual setup procedure:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span>
</pre></div>
<div class="section" id="validation-at-parse-time">
<h1>Validation at parse time</h1>
<p>The parser in lxml can do on-the-fly validation of a document against
a DTD or an XML schema. The DTD is retrieved automatically based on
the DOCTYPE of the parsed document. All you have to do is use a
parser that has DTD validation enabled:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">dtd_validation</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
<p>Obviously, a request for validation enables the DTD loading feature.
There are two other options that enable loading the DTD, but that do
not perform any validation. The first is the <tt class="docutils literal">load_dtd</tt> keyword
option, which simply loads the DTD into the parser and makes it
available to the document as external subset. You can retrieve the
DTD from the parsed document using the <tt class="docutils literal">docinfo</tt> property of the
result ElementTree object. The internal subset is available as
<tt class="docutils literal">internalDTD</tt>, the external subset is provided as <tt class="docutils literal">externalDTD</tt>.</p>
<p>The third way way to activate DTD loading is with the
<tt class="docutils literal">attribute_defaults</tt> option, which loads the DTD and weaves
attribute default values into the document. Again, no validation is
performed unless explicitly requested.</p>
<p>XML schema is supported in a similar way, but requires an explicit
schema to be provided:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">schema_root</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"></span>
<span class="gp">... </span><span class="s"> <xsd:element name="a" type="xsd:integer"/></span>
<span class="gp">... </span><span class="s"> </xsd:schema></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schema</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLSchema</span><span class="p">(</span><span class="n">schema_root</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">root</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"<a>5</a>"</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
</pre></div>
<p>If the validation fails (be it for a DTD or an XML schema), the parser
will raise an exception:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">root</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"<a>no int</a>"</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="gr">lxml.etree.XMLSyntaxError</span>: <span class="n">Element 'a': 'no int' is not a valid value of the atomic type 'xs:integer'.</span>
</pre></div>
<p>If you want the parser to succeed regardless of the outcome of the
validation, you should use a non validating parser and run the
validation separately after parsing the document.</p>
</div>
<div class="section" id="id1">
<h1>DTD</h1>
<p>As described above, the parser support for DTDs depends on internal or
external subsets of the XML file. This means that the XML file itself
must either contain a DTD or must reference a DTD to make this work.
If you want to validate an XML document against a DTD that is not
referenced by the document itself, you can use the <tt class="docutils literal">DTD</tt> class.</p>
<p>To use the <tt class="docutils literal">DTD</tt> class, you must first pass a filename or file-like object
into the constructor to parse a DTD:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">"<!ELEMENT b EMPTY>"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">dtd</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">DTD</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
<p>Now you can use it to validate documents:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">root</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<b/>"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">dtd</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">root</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">root</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">"<b><a/></b>"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">dtd</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">root</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
<p>The reason for the validation failure can be found in the error log:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">dtd</span><span class="o">.</span><span class="n">error_log</span><span class="o">.</span><span class="n">filter_from_errors</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
<span class="go"><string>:1:0:ERROR:VALID:DTD_NOT_EMPTY: Element b was declared EMPTY this one has content</span>
</pre></div>
<p>As an alternative to parsing from a file, you can use the
<tt class="docutils literal">external_id</tt> keyword argument to parse from a catalog. The
following example reads the DocBook DTD in version 4.2, if available
in the system catalog:</p>
<div class="syntax"><pre><span class="n">dtd</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">DTD</span><span class="p">(</span><span class="n">external_id</span> <span class="o">=</span> <span class="s">"-//OASIS//DTD DocBook XML V4.2//EN"</span><span class="p">)</span>
</pre></div>
<p>The DTD information is available as attributes on the DTD object. The method
<tt class="docutils literal">iterelements</tt> provides an iterator over the element declarations:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">dtd</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">DTD</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="s">'<!ELEMENT a EMPTY><!ELEMENT b EMPTY>'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">dtd</span><span class="o">.</span><span class="n">iterelements</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="n">el</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">a</span>
<span class="go">b</span>
</pre></div>
<p>The method <tt class="docutils literal">elements</tt> returns the element declarations as a list:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">dtd</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">DTD</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="s">'<!ELEMENT a EMPTY><!ELEMENT b EMPTY>'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">dtd</span><span class="o">.</span><span class="n">elements</span><span class="p">())</span>
<span class="go">2</span>
</pre></div>
<p>An element declaration object provides the following attributes/methods:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal">name</tt>: The name of the element;</li>
<li><tt class="docutils literal">type</tt>: The element type, one of "undefined", "empty", "any", "mixed" or "element";</li>
<li><tt class="docutils literal">content</tt>: Element content declaration (see below);</li>
<li><tt class="docutils literal">iterattributes()</tt>: Return an iterator over attribute declarations (see below);</li>
<li><tt class="docutils literal">attributes()</tt>: Return a list of attribute declarations.</li>
</ul>
</blockquote>
<p>The <tt class="docutils literal">content</tt> attribute contains information about the content model of the element.
These element content declaration objects form a binary tree (via the <tt class="docutils literal">left</tt> and <tt class="docutils literal">right</tt>
attributes), that makes it possible to reconstruct the content model expression. Here's a
list of all attributes:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal">name</tt>: If this object represents an element in the content model expression,
<tt class="docutils literal">name</tt> is the name of the element, otherwise it is <tt class="docutils literal">None</tt>;</li>
<li><tt class="docutils literal">type</tt>: The type of the node: one of "pcdata", "element", "seq" or "or";</li>
<li><tt class="docutils literal">occur</tt>: How often this element (or this combination of elements) may occur:
one of "once", "opt", "mult" or "plus"</li>
<li><tt class="docutils literal">left</tt>: The left hand subexpression</li>
<li><tt class="docutils literal">right</tt>: The right hand subexpression</li>
</ul>
</blockquote>
<p>For example, the element declaration <tt class="docutils literal"><!ELEMENT a <span class="pre">(a|b)+></span></tt> results
in the following element content declaration objects:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">dtd</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">DTD</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="s">'<!ELEMENT a (a|b)+>'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">content</span> <span class="o">=</span> <span class="n">dtd</span><span class="o">.</span><span class="n">elements</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">content</span>
<span class="gp">>>> </span><span class="n">content</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">content</span><span class="o">.</span><span class="n">occur</span><span class="p">,</span> <span class="n">content</span><span class="o">.</span><span class="n">name</span>
<span class="go">('or', 'plus', None)</span>
<span class="gp">>>> </span><span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o">=</span> <span class="n">content</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">content</span><span class="o">.</span><span class="n">right</span>
<span class="gp">>>> </span><span class="n">left</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">left</span><span class="o">.</span><span class="n">occur</span><span class="p">,</span> <span class="n">left</span><span class="o">.</span><span class="n">name</span>
<span class="go">('element', 'once', 'a')</span>
<span class="gp">>>> </span><span class="n">right</span><span class="o">.</span><span class="n">type</span><span class="p">,</span> <span class="n">right</span><span class="o">.</span><span class="n">occur</span><span class="p">,</span> <span class="n">right</span><span class="o">.</span><span class="n">name</span>
<span class="go">('element', 'once', 'b')</span>
</pre></div>
<p>Attributes declarations have the following attributes/methods:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal">name</tt>: The name of the attribute;</li>
<li><tt class="docutils literal">elemname</tt>: The name of the element the attribute belongs to;</li>
<li><tt class="docutils literal">type</tt>: The attribute type, one of "cdata", "id", "idref", "idrefs", "entity",
"entities", "nmtoken", "nmtokens", "enumeration" or "notation";</li>
<li><tt class="docutils literal">default</tt>: The type of the default value, one of "none", "required", "implied"
or "fixed";</li>
<li><tt class="docutils literal">defaultValue</tt>: The default value;</li>
<li><tt class="docutils literal">itervalues()</tt>: Return an iterator over the allowed attribute values (if the attribute
if of type "enumeration");</li>
<li><tt class="docutils literal">values()</tt>: Return a list of allowed attribute values.</li>
</ul>
</blockquote>
<p>Entity declarations are available via the <tt class="docutils literal">iterentities</tt> and <tt class="docutils literal">entities</tt> methods:</p>
<blockquote>
<pre class="doctest-block">
>>> dtd = etree.DTD(StringIO('<!ENTITY hurz "&#x40;">'))
>>> entity = dtd.entities()[0]
>>> entity.name, entity.orig, entity.content
('hurz', '&#x40;', '@')
</pre>
</blockquote>
</div>
<div class="section" id="relaxng">
<h1>RelaxNG</h1>
<p>The <tt class="docutils literal">RelaxNG</tt> class takes an ElementTree object to construct a Relax NG
validator:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><element name="a" xmlns="http://relaxng.org/ns/structure/1.0"></span>
<span class="gp">... </span><span class="s"> <zeroOrMore></span>
<span class="gp">... </span><span class="s"> <element name="b"></span>
<span class="gp">... </span><span class="s"> <text /></span>
<span class="gp">... </span><span class="s"> </element></span>
<span class="gp">... </span><span class="s"> </zeroOrMore></span>
<span class="gp">... </span><span class="s"></element></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">relaxng_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">relaxng</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">RelaxNG</span><span class="p">(</span><span class="n">relaxng_doc</span><span class="p">)</span>
</pre></div>
<p>Alternatively, pass a filename to the <tt class="docutils literal">file</tt> keyword argument to parse from
a file. This also enables correct handling of include files from within the
RelaxNG parser.</p>
<p>You can then validate some ElementTree document against the schema. You'll get
back True if the document is valid against the Relax NG schema, and False if
not:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">valid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><b></b></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">relaxng</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">invalid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><c></c></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc2</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">invalid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">relaxng</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>Calling the schema object has the same effect as calling its validate
method. This is sometimes used in conditional statements:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">invalid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><c></c></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc2</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">invalid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="ow">not</span> <span class="n">relaxng</span><span class="p">(</span><span class="n">doc2</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">"invalid!"</span><span class="p">)</span>
<span class="go">invalid!</span>
</pre></div>
<p>If you prefer getting an exception when validating, you can use the
<tt class="docutils literal">assert_</tt> or <tt class="docutils literal">assertValid</tt> methods:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">relaxng</span><span class="o">.</span><span class="n">assertValid</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">lxml.etree.DocumentInvalid</span>: <span class="n">Did not expect element c there, line 1</span>
<span class="gp">>>> </span><span class="n">relaxng</span><span class="o">.</span><span class="n">assert_</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">Did not expect element c there, line 1</span>
</pre></div>
<p>If you want to find out why the validation failed in the second case, you can
look up the error log of the validation process and check it for relevant
messages:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">log</span> <span class="o">=</span> <span class="n">relaxng</span><span class="o">.</span><span class="n">error_log</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">log</span><span class="o">.</span><span class="n">last_error</span><span class="p">)</span>
<span class="go"><string>:1:0:ERROR:RELAXNGV:RELAXNG_ERR_ELEMWRONG: Did not expect element c there</span>
</pre></div>
<p>You can see that the error (ERROR) happened during RelaxNG validation
(RELAXNGV). The message then tells you what went wrong. You can also
look at the error domain and its type directly:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">error</span> <span class="o">=</span> <span class="n">log</span><span class="o">.</span><span class="n">last_error</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">domain_name</span><span class="p">)</span>
<span class="go">RELAXNGV</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">type_name</span><span class="p">)</span>
<span class="go">RELAXNG_ERR_ELEMWRONG</span>
</pre></div>
<p>Note that this error log is local to the RelaxNG object. It will only
contain log entries that appeared during the validation.</p>
<p>Similar to XSLT, there's also a less efficient but easier shortcut method to
do one-shot RelaxNG validation:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">doc</span><span class="o">.</span><span class="n">relaxng</span><span class="p">(</span><span class="n">relaxng_doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">doc2</span><span class="o">.</span><span class="n">relaxng</span><span class="p">(</span><span class="n">relaxng_doc</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>libxml2 does not currently support the <a class="reference external" href="http://www.thaiopensource.com/relaxng/trang.html">RelaxNG Compact Syntax</a>.
However, the <a class="reference external" href="http://www.thaiopensource.com/relaxng/trang.html">trang</a> translator can convert the compact syntax to the
XML syntax, which can then be used with lxml.</p>
</div>
<div class="section" id="xmlschema">
<h1>XMLSchema</h1>
<p>lxml.etree also has XML Schema (XSD) support, using the class
lxml.etree.XMLSchema. The API is very similar to the Relax NG and DTD
classes. Pass an ElementTree object to construct a XMLSchema validator:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"></span>
<span class="gp">... </span><span class="s"><xsd:element name="a" type="AType"/></span>
<span class="gp">... </span><span class="s"><xsd:complexType name="AType"></span>
<span class="gp">... </span><span class="s"> <xsd:sequence></span>
<span class="gp">... </span><span class="s"> <xsd:element name="b" type="xsd:string" /></span>
<span class="gp">... </span><span class="s"> </xsd:sequence></span>
<span class="gp">... </span><span class="s"></xsd:complexType></span>
<span class="gp">... </span><span class="s"></xsd:schema></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">xmlschema_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">xmlschema</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLSchema</span><span class="p">(</span><span class="n">xmlschema_doc</span><span class="p">)</span>
</pre></div>
<p>You can then validate some ElementTree document with this. Like with RelaxNG,
you'll get back true if the document is valid against the XML schema, and
false if not:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">valid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><b></b></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">xmlschema</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">invalid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><c></c></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc2</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">invalid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">xmlschema</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>Calling the schema object has the same effect as calling its validate method.
This is sometimes used in conditional statements:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">invalid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'<a><c></c></a>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc2</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">invalid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="ow">not</span> <span class="n">xmlschema</span><span class="p">(</span><span class="n">doc2</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">"invalid!"</span><span class="p">)</span>
<span class="go">invalid!</span>
</pre></div>
<p>If you prefer getting an exception when validating, you can use the
<tt class="docutils literal">assert_</tt> or <tt class="docutils literal">assertValid</tt> methods:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">xmlschema</span><span class="o">.</span><span class="n">assertValid</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">lxml.etree.DocumentInvalid</span>: <span class="n">Element 'c': This element is not expected. Expected is ( b )., line 1</span>
<span class="gp">>>> </span><span class="n">xmlschema</span><span class="o">.</span><span class="n">assert_</span><span class="p">(</span><span class="n">doc2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">Element 'c': This element is not expected. Expected is ( b )., line 1</span>
</pre></div>
<p>Error reporting works as for the RelaxNG class:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">log</span> <span class="o">=</span> <span class="n">xmlschema</span><span class="o">.</span><span class="n">error_log</span>
<span class="gp">>>> </span><span class="n">error</span> <span class="o">=</span> <span class="n">log</span><span class="o">.</span><span class="n">last_error</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">domain_name</span><span class="p">)</span>
<span class="go">SCHEMASV</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="o">.</span><span class="n">type_name</span><span class="p">)</span>
<span class="go">SCHEMAV_ELEMENT_CONTENT</span>
</pre></div>
<p>If you were to print this log entry, you would get something like the
following. Note that the error message depends on the libxml2 version in
use:</p>
<pre class="literal-block">
<string>:1:ERROR::SCHEMAV_ELEMENT_CONTENT: Element 'c': This element is not expected. Expected is ( b ).
</pre>
<p>Similar to XSLT and RelaxNG, there's also a less efficient but easier shortcut
method to do XML Schema validation:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">doc</span><span class="o">.</span><span class="n">xmlschema</span><span class="p">(</span><span class="n">xmlschema_doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">doc2</span><span class="o">.</span><span class="n">xmlschema</span><span class="p">(</span><span class="n">xmlschema_doc</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="section" id="id2">
<h1>Schematron</h1>
<p>From version 2.3 on lxml features ISO-<a class="reference external" href="http://www.schematron.com">Schematron</a> support built on the
de-facto reference implementation of Schematron, the pure-XSLT-1.0
<a class="reference external" href="http://www.schematron.com/implementation.html">skeleton implementation</a>. This is provided by the lxml.isoschematron package
that implements the Schematron class, with an API compatible to the other
validators'. Pass an Element or ElementTree object to construct a Schematron
validator:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">isoschematron</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><schema xmlns="http://purl.oclc.org/dsdl/schematron" ></span>
<span class="gp">... </span><span class="s"> <pattern id="sum_equals_100_percent"></span>
<span class="gp">... </span><span class="s"> <title>Sum equals 100%.</title></span>
<span class="gp">... </span><span class="s"> <rule context="Total"></span>
<span class="gp">... </span><span class="s"> <assert test="sum(//Percent)=100">Sum is not 100%.</assert></span>
<span class="gp">... </span><span class="s"> </rule></span>
<span class="gp">... </span><span class="s"> </pattern></span>
<span class="gp">... </span><span class="s"></schema></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">sct_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span> <span class="o">=</span> <span class="n">isoschematron</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">)</span>
</pre></div>
<p>You can then validate some ElementTree document with this. Just like with
XMLSchema or RelaxNG, you'll get back true if the document is valid against the
schema, and false if not:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">valid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><Total></span>
<span class="gp">... </span><span class="s"> <Percent>20</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>30</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>50</Percent></span>
<span class="gp">... </span><span class="s"></Total></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">etree</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">doc</span><span class="o">.</span><span class="n">getroot</span><span class="p">(),</span> <span class="s">"Percent"</span><span class="p">)</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="s">"10"</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>Calling the schema object has the same effect as calling its validate method.
This can be useful for conditional statements:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">is_valid</span> <span class="o">=</span> <span class="n">isoschematron</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="ow">not</span> <span class="n">is_valid</span><span class="p">(</span><span class="n">doc</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">"invalid!"</span><span class="p">)</span>
<span class="go">invalid!</span>
</pre></div>
<p>Built on a pure-xslt implementation, the actual validator is created as an
XSLT 1.0 stylesheet using these steps:</p>
<ol class="arabic simple" start="0">
<li>(Extract embedded Schematron from XML Schema or RelaxNG schema)</li>
<li>Process inclusions</li>
<li>Process abstract patterns</li>
<li>Compile the schematron schema to XSLT</li>
</ol>
<p>To allow more control over the individual steps, isoschematron.Schematron
supports an extended API:</p>
<p>The <tt class="docutils literal">include</tt> and <tt class="docutils literal">expand</tt> keyword arguments can be used to switch off
steps 1) and 2).</p>
<p>To set parameters for steps 1), 2) and 3) dictionaries containing parameters
for XSLT can be provided using the keyword arguments <tt class="docutils literal">include_params</tt>,
<tt class="docutils literal">expand_params</tt> or <tt class="docutils literal">compile_params</tt>. Schematron automatically converts these
parameters to stylesheet parameters so you need not worry to set string
parameters using quotes or to use XSLT.strparam(). If you ever need to pass an
XPath as argument to the XSLT stylesheet you can pass in an etree.XPath object
(see XPath and XSLT with lxml: <a class="reference external" href="xpathxslt.html#stylesheet-parameters">Stylesheet-parameters</a> for background on this).</p>
<p>The <tt class="docutils literal">phase</tt> parameter of the compile step is additionally exposed as a keyword
argument. If set, it overrides occurrence in <tt class="docutils literal">compile_params</tt>. Note that
isoschematron.Schematron might expose more common parameters as additional keyword
args in the future.</p>
<p>By setting <tt class="docutils literal">store_schematron</tt> to True, the (included-and-expanded) schematron
document tree is stored and made available through the <tt class="docutils literal">schematron</tt> property.</p>
<p>Similarly, setting <tt class="docutils literal">store_xslt</tt> to True will result in the validation XSLT
document tree being kept; it can be retrieved through the <tt class="docutils literal">validator_xslt</tt>
property.</p>
<p>Finally, with <tt class="docutils literal">store_report</tt> set to True (default: False), the resulting
validation report document gets stored and can be accessed as the
<tt class="docutils literal">validation_report</tt> property.</p>
<p>Using the <tt class="docutils literal">phase</tt> parameter of isoschematron.Schematron allows for selective
validation of predefined pattern groups:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><schema xmlns="http://purl.oclc.org/dsdl/schematron" ></span>
<span class="gp">... </span><span class="s"> <phase id="phase.sum_check"></span>
<span class="gp">... </span><span class="s"> <active pattern="sum_equals_100_percent"/></span>
<span class="gp">... </span><span class="s"> </phase></span>
<span class="gp">... </span><span class="s"> <phase id="phase.entries_check"></span>
<span class="gp">... </span><span class="s"> <active pattern="all_positive"/></span>
<span class="gp">... </span><span class="s"> </phase></span>
<span class="gp">... </span><span class="s"> <pattern id="sum_equals_100_percent"></span>
<span class="gp">... </span><span class="s"> <title>Sum equals 100%.</title></span>
<span class="gp">... </span><span class="s"> <rule context="Total"></span>
<span class="gp">... </span><span class="s"> <assert test="sum(//Percent)=100">Sum is not 100%.</assert></span>
<span class="gp">... </span><span class="s"> </rule></span>
<span class="gp">... </span><span class="s"> </pattern></span>
<span class="gp">... </span><span class="s"> <pattern id="all_positive"></span>
<span class="gp">... </span><span class="s"> <title>All entries must be positive.</title></span>
<span class="gp">... </span><span class="s"> <rule context="Percent"></span>
<span class="gp">... </span><span class="s"> <assert test="number(.)>0">Number (<value-of select="."/>) not positive</assert></span>
<span class="gp">... </span><span class="s"> </rule></span>
<span class="gp">... </span><span class="s"> </pattern></span>
<span class="gp">... </span><span class="s"></schema></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">sct_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span> <span class="o">=</span> <span class="n">isoschematron</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">valid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><Total></span>
<span class="gp">... </span><span class="s"> <Percent>20</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>30</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>50</Percent></span>
<span class="gp">... </span><span class="s"></Total></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">invalid_positive</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><Total></span>
<span class="gp">... </span><span class="s"> <Percent>0</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>50</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>50</Percent></span>
<span class="gp">... </span><span class="s"></Total></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">invalid_positive</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>If the constraint of Percent entries being positive is not of interest in a
certain validation scenario, it can now be disabled:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">selective</span> <span class="o">=</span> <span class="n">isoschematron</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">,</span> <span class="n">phase</span><span class="o">=</span><span class="s">"phase.sum_check"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">selective</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
<p>The usage of validation phases is a unique feature of ISO-Schematron and can be
a very powerful tool e.g. for establishing validation stages or to provide
different validators for different "validation audiences".</p>
</div>
<div class="section" id="id3">
<h1>(Pre-ISO-Schematron)</h1>
<p>Since version 2.0, lxml.etree features <a class="reference external" href="http://www.ascc.net/xml/schematron">pre-ISO-Schematron</a> support, using the
class lxml.etree.Schematron. It requires at least libxml2 2.6.21 to
work. The API is the same as for the other validators. Pass an
ElementTree object to construct a Schematron validator:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><schema xmlns="http://www.ascc.net/xml/schematron" ></span>
<span class="gp">... </span><span class="s"> <pattern name="Sum equals 100%."></span>
<span class="gp">... </span><span class="s"> <rule context="Total"></span>
<span class="gp">... </span><span class="s"> <assert test="sum(//Percent)=100">Sum is not 100%.</assert></span>
<span class="gp">... </span><span class="s"> </rule></span>
<span class="gp">... </span><span class="s"> </pattern></span>
<span class="gp">... </span><span class="s"></schema></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">sct_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">)</span>
</pre></div>
<p>You can then validate some ElementTree document with this. Like with RelaxNG,
you'll get back true if the document is valid against the schema, and false if
not:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">valid</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
<span class="gp">... </span><span class="s"><Total></span>
<span class="gp">... </span><span class="s"> <Percent>20</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>30</Percent></span>
<span class="gp">... </span><span class="s"> <Percent>50</Percent></span>
<span class="gp">... </span><span class="s"></Total></span>
<span class="gp">... </span><span class="s">'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">etree</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">doc</span><span class="o">.</span><span class="n">getroot</span><span class="p">(),</span> <span class="s">"Percent"</span><span class="p">)</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="s">"10"</span>
<span class="gp">>>> </span><span class="n">schematron</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
<p>Calling the schema object has the same effect as calling its validate method.
This is sometimes used in conditional statements:</p>
<div class="syntax"><pre><span class="gp">>>> </span><span class="n">is_valid</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">Schematron</span><span class="p">(</span><span class="n">sct_doc</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="ow">not</span> <span class="n">is_valid</span><span class="p">(</span><span class="n">doc</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">"invalid!"</span><span class="p">)</span>
<span class="go">invalid!</span>
</pre></div>
<p>Note that libxml2 restricts error reporting to the parsing step (when creating
the Schematron instance). There is not currently any support for error
reporting during validation.</p>
</div>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2015-11-13.
</div>
</body>
</html>
|