From 5334eb3c62f3820ccc0251fda92308c19b371fbe Mon Sep 17 00:00:00 2001 From: Sergred Date: Tue, 9 Sep 2014 23:06:35 +0400 Subject: [PATCH 1/4] Added xml file --- xml/mephi.xml | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 xml/mephi.xml diff --git a/xml/mephi.xml b/xml/mephi.xml new file mode 100644 index 0000000..ffcb403 --- /dev/null +++ b/xml/mephi.xml @@ -0,0 +1,41 @@ + + + + + + Артемьев Дмитрий + Анисимова Наталья + Бубенко Кирилл + Джелоухова Алена + Заманов Айнур + Михеев Денис + Пивоваров Александр + Самсонов Артем + Соловьева Аня + Суханова Любовь + Тармазаков Евгений + Титоренко Алексей + Штанько Александр + + + Ахметсафин Владислав + Галкин Александр + Головко Ирина + Джумайло Евгений + Ерохин Владимир + Каталкина Виктория + Левин Андрей + Молочков Ярослав + Моряшова Виктория + Полстянкин Константин + Пурик Яна + Разживин Никита + Редюк Сергей + Рябов Петр + Скок Дарья + Стрекалов Олег + Чухненко Александра + + + + From 5879fec2bdc7efc5c016591bf95c068c428896ae Mon Sep 17 00:00:00 2001 From: Sergred Date: Tue, 9 Sep 2014 23:07:14 +0400 Subject: [PATCH 2/4] Added printing script --- xml/parser.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 xml/parser.py diff --git a/xml/parser.py b/xml/parser.py new file mode 100644 index 0000000..999c1a5 --- /dev/null +++ b/xml/parser.py @@ -0,0 +1,42 @@ +#! /usr/bin/env python +import sys, libxml2 + +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 main(argv): + if len(argv) != 2: + sys.stderr.write("Usage : %s xml_file" % (argv[0],)) + else: open(argv[1]) + +if __name__ == '__main__': + main(sys.argv) \ No newline at end of file From a2c0974d011ba4c3da5305362d22d1ca78ebe000 Mon Sep 17 00:00:00 2001 From: Sergred Date: Tue, 16 Sep 2014 16:19:02 +0400 Subject: [PATCH 3/4] Added dtd file --- xml/mephi.dtd | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 xml/mephi.dtd diff --git a/xml/mephi.dtd b/xml/mephi.dtd new file mode 100644 index 0000000..af7b084 --- /dev/null +++ b/xml/mephi.dtd @@ -0,0 +1,8 @@ + + + + + + + + From fab5ea2ed4ba8570cdf96014cfb20fbdd3cc91a6 Mon Sep 17 00:00:00 2001 From: Sergred Date: Tue, 16 Sep 2014 16:19:59 +0400 Subject: [PATCH 4/4] Added validating function --- xml/parser.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/xml/parser.py b/xml/parser.py index 999c1a5..1c749f3 100644 --- a/xml/parser.py +++ b/xml/parser.py @@ -1,5 +1,6 @@ #! /usr/bin/env python -import sys, libxml2 +#-*- coding: UTF-8 -*- +import sys, libxml2, optparse def space(dep): for i in range(0, dep): @@ -33,10 +34,24 @@ def open(xml_file): 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): - if len(argv) != 2: - sys.stderr.write("Usage : %s xml_file" % (argv[0],)) - else: open(argv[1]) + 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) \ No newline at end of file