Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xsd #20

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Xsd #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions mephi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<university name="Mephi" city="Moscow" metro="Kashirskaya"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.w3schools.com mephi.xsd">
<faculty name="KIB" dean="Evseev">
<department name="Information systems and technologies" number="36">
<group name="K5361" amount="13">
<student>Anisimova</student>
<student>Artemyev</student>
<student>Bubenko</student>
<student>Dzheloukhova</student>
<student>Zamanov</student>
<student>Miheev</student>
<student>Pivovarov</student>
<student>Samsonov</student>
<student>Sidorova</student>
<student>Solovyeva</student>
<student>Tarmazakov</student>
<student>Titorenko</student>
<student>Shtanko</student>
</group>
</department>
</faculty>
</university>
59 changes: 59 additions & 0 deletions mephi.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="nametype">
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z0-9 ])*"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="studenttype">
<xs:restriction base="xs:string">
<xs:pattern value="([a-zA-Z])*"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="numbertype">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="40"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="universitytype">
<xs:sequence>
<xs:element name="faculty" type="facultytype"/>
</xs:sequence>
<xs:attribute name="name" type="nametype" use="required"/>
<xs:attribute name="city" type="nametype" use="required"/>
<xs:attribute name="metro" type="nametype" use="required"/>
</xs:complexType>

<xs:complexType name="facultytype">
<xs:sequence>
<xs:element name="department" type="departmenttype"/>
</xs:sequence>
<xs:attribute name="name" type="nametype" use="required"/>
<xs:attribute name="dean" type="nametype" use="required"/>
</xs:complexType>

<xs:complexType name="departmenttype">
<xs:sequence>
<xs:element name="group" type="grouptype"/>
</xs:sequence>
<xs:attribute name="name" type="nametype" use="required"/>
<xs:attribute name="number" type="numbertype" use="required"/>
</xs:complexType>

<xs:complexType name="grouptype">
<xs:sequence>
<xs:element name="student" type="studenttype"
maxOccurs="13" minOccurs="1"/>
</xs:sequence>
<xs:attribute name="name" type="nametype" use="required"/>
<xs:attribute name="amount" type="numbertype" use="required"/>
</xs:complexType>

<xs:element name="university" type="universitytype"/>
<xs:element name="университет" substitutionGroup="university"/>
</xs:schema>
30 changes: 30 additions & 0 deletions validxsd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python
#-*- coding: UTF-8 -*-

import optparse, libxml2, sys

def schema_validate(xml_file, xsd_file):
ctxt = libxml2.schemaNewParserCtxt(xsd_file)
schema = ctxt.schemaParse()
validationCtxt = schema.schemaNewValidCtxt()
res = validationCtxt.schemaValidateFile(xml_file, 0)
if res != 0:
print "VALIDATION FAILED"
else:
print "VALIDATED"
return res


def main(argv):
op = optparse.OptionParser(description = U"Проверка на соответствие XSD", prog="xsd", version="0.1", usage=U"%prog")
op.add_option("-x", "--xml", dest="xml", help=U"XML документ", metavar="XML_FILE")
op.add_option("-s", "--xsd", dest="xsd", help=U"XSD документ", metavar="XSD_FILE")

options, arguments = op.parse_args()
if options.xml and options.xsd:
schema_validate(options.xml, options.xsd)
else:
op.print_help()

if __name__=='__main__':
main(sys.argv)