Skip to content
Open

Dtd #12

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
8 changes: 8 additions & 0 deletions xml/mephi.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!ELEMENT mephi (faculty+)>
<!ELEMENT faculty (department+)>
<!ELEMENT department (group+)>
<!ELEMENT group (student+)>
<!ELEMENT student (#PCDATA)>
<!ATTLIST faculty name CDATA #REQUIRED>
<!ATTLIST department number CDATA #REQUIRED>
<!ATTLIST group grnum CDATA #REQUIRED>
41 changes: 41 additions & 0 deletions xml/mephi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<mephi>
<faculty name="K">
<department number="36">
<group grnum="1">
<student>Артемьев Дмитрий</student>
<student>Анисимова Наталья</student>
<student>Бубенко Кирилл</student>
<student>Джелоухова Алена</student>
<student>Заманов Айнур</student>
<student>Михеев Денис</student>
<student>Пивоваров Александр</student>
<student>Самсонов Артем</student>
<student>Соловьева Аня</student>
<student>Суханова Любовь</student>
<student>Тармазаков Евгений</student>
<student>Титоренко Алексей</student>
<student>Штанько Александр</student>
</group>
<group grnum="2">
<student>Ахметсафин Владислав</student>
<student>Галкин Александр</student>
<student>Головко Ирина</student>
<student>Джумайло Евгений</student>
<student>Ерохин Владимир</student>
<student>Каталкина Виктория</student>
<student>Левин Андрей</student>
<student>Молочков Ярослав</student>
<student>Моряшова Виктория</student>
<student>Полстянкин Константин</student>
<student>Пурик Яна</student>
<student>Разживин Никита</student>
<student>Редюк Сергей</student>
<student>Рябов Петр</student>
<student>Скок Дарья</student>
<student>Стрекалов Олег</student>
<student>Чухненко Александра</student>
</group>
</department>
</faculty>
</mephi>
57 changes: 57 additions & 0 deletions xml/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#! /usr/bin/env python
#-*- coding: UTF-8 -*-
import sys, libxml2, optparse

def space(dep):
for i in range(0, dep):
sys.stdout.write(" ")

depth = 0
def view(node):
global depth
print
space(depth)
sys.stdout.write(node.name+" ")
if node.properties is not None:
for _property in node.properties:
if _property.type == "attribute":
#print _property.name
sys.stdout.write(_property.content+" ")
child = node.children
depth = depth+1
while child.next is not None:
if child.type == "element":
view(child)
child = child.next
depth = depth-1
sys.stdout.write(child.content)

def open(xml_file):
doc = libxml2.parseFile(xml_file)
root = doc.getRootElement()
#print root.name
#print root.content
view(root)
doc.freeDoc()

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(argv):
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:
open(options.xml)
validate(options.xml, options.dtd)
else: op.print_help()

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