Skip to content
Open
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
13 changes: 13 additions & 0 deletions mephi.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!ELEMENT university (faculty+)>
<!ELEMENT faculty (department+)>
<!ELEMENT department (group+)>
<!ELEMENT group (student+)>
<!ELEMENT student EMPTY>
<!ATTLIST university
name CDATA #REQUIRED
found CDATA #IMPLIED
>
<!ATTLIST faculty name ID #REQUIRED>
<!ATTLIST department number ID #REQUIRED>
<!ATTLIST group number ID #REQUIRED>
<!ATTLIST student name ID #REQUIRED>
13 changes: 13 additions & 0 deletions mephi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "mephi.dtd">
<model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./mephi.xsd" >
<university name="МИФИ" found="1942">
<faculty name="КиБ">
<department number="36">
<group name="К05-361">
<student name="Чкадуа Антон Витальевич"/>
</group>
</department>
</faculty>
</university>
34 changes: 34 additions & 0 deletions mephi.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="universityInfo">
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="found" type="xs:positiveInteger"/>
</xs:complexType>

<xs:complexType name="man">
<xs:attribute name="name" type="xs:string"/>
<
<xs:element name="university" type="universityInfo">
<xs:complexType>
<xs:element name="faculty">
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:whitespace value="preserved"/>
<xs:complexType>
<xs:element name="department">
<xs:attribute name="number" type="xs:positiveInteger" use="required"/>
<xs:complexType>
<xs:element name="group">
<xs:attribute name="name" use="required"/>
<xs:complexType>
<xs:element name="student" minOccurs="0" maxOccurs="25">
<xs:attribute name="name"/>
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
19 changes: 19 additions & 0 deletions schema_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env python
import sys, libxml2

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

if __name__ == ' __main__ ':
main(sys.argv)
41 changes: 41 additions & 0 deletions xml_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import libxml2

def getPropValue(node,name):
for prop in node.properties:
if prop.type == 'attribute' and prop.name == 'name':
return prop.content

def main(argv):
if len(argv) != 2:
sys.stderr.write("Usage : %s xml_file" % (argv[0],))
else:
doc = libxml2.parseFile(argv[1])
university = doc.getRootElement()
print(getPropValue(university,'name'))
faculty = university.children
while faculty is not None:
if faculty.type == 'element':
print(" Факультет: " + getPropValue(faculty,'name'))
group = faculty.children
while department is not None:
if department.type == 'element':
print(" Кафедра №" + getPropValue(department,'name'))
group = department.children
while geoup is not None:
if group.type == 'element':
print(" Группа " + getPropValue(group,'name'))
student = group.children
while student is not None:
if student.type == 'element':
print(" " + getPropValue(student,'name'))
student = student.next
group = group.next
department = department.next
faculty = faculty.next
doc.freeDoc()

if __name__ == ' __main__ ':
main(sys.argv)
29 changes: 29 additions & 0 deletions xml_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import libxml2
import optparse

def validate(xml_file, dtd_file):
doc = libxml2.parseFile(xml_file)
dtd = libxml2.parseDTD(None, dtd_file)
ctxt = libxml2.newValidCtxt()
ret = doc.validateDtd(ctxt, dtd)
dtd.freeDtd()
doc.freeDoc()
return ret

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

options, arguments = op.parse_args()
if options.xml and options.dtd:
validate(options.xml, options.dtd)
else:
op.print_help()

if __name__ == ' __main__ ':
main()