Created
November 27, 2017 19:56
-
-
Save ruffsl/db63d2d0c9845ae3b1c4a444c01bfd12 to your computer and use it in GitHub Desktop.
xmlschema errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.9.14\n" | |
] | |
} | |
], | |
"source": [ | |
"import xmlschema\n", | |
"print(xmlschema.__version__)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xsd_string = \"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n", | |
" elementFormDefault=\"qualified\"\n", | |
" attributeFormDefault=\"unqualified\">\n", | |
"\n", | |
" <xs:element name=\"foo\" type=\"Foo\" />\n", | |
"\n", | |
" <xs:complexType name=\"Foo\">\n", | |
" <xs:sequence minOccurs=\"1\" maxOccurs=\"unbounded\">\n", | |
" <xs:element name=\"bar\" type=\"Bar\" />\n", | |
" </xs:sequence>\n", | |
" </xs:complexType>\n", | |
"\n", | |
" <xs:complexType name=\"Bar\">\n", | |
" <xs:sequence minOccurs=\"1\" maxOccurs=\"1\">\n", | |
" <xs:element name=\"subject_name\" type=\"xs:string\" />\n", | |
" </xs:sequence>\n", | |
" <xs:attribute name=\"name\" fixed=\"BAR\" use=\"required\"/>\n", | |
" </xs:complexType>\n", | |
"</xs:schema>\n", | |
"\"\"\"\n", | |
"xsd_schema = xmlschema.XMLSchema(xsd_string)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xml_string_1 = \"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
"<foo>\n", | |
" <bar name=\"BAR\">\n", | |
" <subject_name>Bar</subject_name>\n", | |
" </bar>\n", | |
"</foo>\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xml_string_2 = \"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
"<foo>\n", | |
" <bar name=\"RAB\">\n", | |
" <subject_name>Bar</subject_name>\n", | |
" </bar>\n", | |
"</foo>\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xml_string_3 = \"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", | |
"<foo>\n", | |
" <bar>\n", | |
" <subject_name>Bar</subject_name>\n", | |
" </bar>\n", | |
"</foo>\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"print(xsd_schema.validate(data=xml_string_1, use_defaults=False))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"print(xsd_schema.validate(data=xml_string_2, use_defaults=False))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "XMLSchemaValidationError", | |
"evalue": "failed validating {} with <XsdAttributeGroup at 0x7f8c506dad30>.\n\nReason: missing required attributes {'name'}\n\nSchema:\n\n <xs:complexType xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" name=\"Bar\">\n <xs:sequence maxOccurs=\"1\" minOccurs=\"1\">\n <xs:element name=\"subject_name\" type=\"xs:string\" />\n </xs:sequence>\n <xs:attribute fixed=\"BAR\" name=\"name\" use=\"required\" />\n </xs:complexType>\n\nInstance:\n\n <bar>\n <subject_name>Bar</subject_name>\n </bar>\n", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mXMLSchemaValidationError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-8-cc56bcfbbb75>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxsd_schema\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalidate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mxml_string_3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muse_defaults\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/xmlschema/xsdbase.py\u001b[0m in \u001b[0;36mvalidate\u001b[0;34m(self, data, use_defaults)\u001b[0m\n\u001b[1;32m 248\u001b[0m \"\"\"\n\u001b[1;32m 249\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0merror\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miter_errors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muse_defaults\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muse_defaults\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 250\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 251\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 252\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0miter_errors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muse_defaults\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mXMLSchemaValidationError\u001b[0m: failed validating {} with <XsdAttributeGroup at 0x7f8c506dad30>.\n\nReason: missing required attributes {'name'}\n\nSchema:\n\n <xs:complexType xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" name=\"Bar\">\n <xs:sequence maxOccurs=\"1\" minOccurs=\"1\">\n <xs:element name=\"subject_name\" type=\"xs:string\" />\n </xs:sequence>\n <xs:attribute fixed=\"BAR\" name=\"name\" use=\"required\" />\n </xs:complexType>\n\nInstance:\n\n <bar>\n <subject_name>Bar</subject_name>\n </bar>\n" | |
] | |
} | |
], | |
"source": [ | |
"print(xsd_schema.validate(data=xml_string_3, use_defaults=False))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment