Jump to navigation. The example used in this blog to demonstrate its title revisits a subject I mentioned in a previous post, the Companies House XML gateway, of which I've had a fair few dealings of recent times.
After a bit of reading around the subject it appeared that the lxml lib fitted the bill for the sorts of things that were going to be needed.
The latter of those "things", validating XML against XML schemas is the subject of this post seeing as when talking to the gateway your messages have to have been validated against, often multiple, schemas. I want to validate the file against that schema and check if it adheres to that. I am using python but am open to any language for that matter if there is no such useful library in python.
What would be my best options here? I would worry about the how fast I can get this up and running. If the schema file contains an xml tag with an encoding e. A solution is to open the files in byte mode: open Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 5 months ago. Active 6 months ago. Viewed 17k times. Improve this question.
Scooby Scooby 2, 6 6 gold badges 40 40 silver badges 78 78 bronze badges. Add a comment. Active Oldest Votes. Definitely lxml. Define an XMLParser with a predefined schema, load the the file fromstring and catch any XML Schema errors: from lxml import etree def validate xmlparser, xmlfilename : try: with open xmlfilename, 'r' as f: etree. In order to validate more than one file, there is no need to create an XMLSchema object every time, therefore:.
For more options read here: Validation with lxml. It's pure Python, available on PyPi and doesn't have many dependencies. The method raises an exception if the file doesn't validate against the XSD. That exception then contains some violation details. Alternatively, xmlschema directly works on file objects and in memory XML trees either created with xml.
ElementTree or lxml. It handles almost every schema construct and supports multiple namespaces. There are two ways actually there are more that you could do this. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 13 years, 1 month ago. Active 6 months ago. Viewed k times. Improve this question. Eli Courtwright Eli Courtwright k 64 64 gold badges silver badges bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. Sorin: lxml is a wrapper on top of the libxml2 C library, and thus is not pure Python.
0コメント