diff --git a/NVIvanov/mephi.dtd b/NVIvanov/mephi.dtd new file mode 100644 index 0000000..25e4610 --- /dev/null +++ b/NVIvanov/mephi.dtd @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NVIvanov/mephi.xml b/NVIvanov/mephi.xml new file mode 100644 index 0000000..64b7edc --- /dev/null +++ b/NVIvanov/mephi.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NVIvanov/mephi.xsd b/NVIvanov/mephi.xsd new file mode 100644 index 0000000..b4c38c2 --- /dev/null +++ b/NVIvanov/mephi.xsd @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NVIvanov/mephi1.py b/NVIvanov/mephi1.py new file mode 100755 index 0000000..f321b01 --- /dev/null +++ b/NVIvanov/mephi1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import libxml2 + + +def parse_students(doc): + mephi = doc.getRootElement() + faculty = mephi.children + while faculty is not None: + if faculty.type == 'element': + department = faculty.children + while department is not None: + if department.type == 'element': + group = department.children + while group is not None: + if group.type == 'element': + student = group.children + while student is not None: + if student.type == 'element': + print(student.prop("name")) + student = student.next + group = group.next + department = department.next + faculty = faculty.next + + +def main(argv): + doc = libxml2.parseFile(argv[1]) + parse_students(doc) + doc.freeDoc() + +if __name__ == '__main__': + main(sys.argv) diff --git a/NVIvanov/mephi2.py b/NVIvanov/mephi2.py new file mode 100644 index 0000000..ef2b731 --- /dev/null +++ b/NVIvanov/mephi2.py @@ -0,0 +1,33 @@ +#!usr/bin/env python +# -*- coding: utf-8 -*- + +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) + if ret == 1: + print(U"Документ удовлетворяет типу") + 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() diff --git a/NVIvanov/mephi3.py b/NVIvanov/mephi3.py new file mode 100644 index 0000000..4ceacbc --- /dev/null +++ b/NVIvanov/mephi3.py @@ -0,0 +1,24 @@ +#!usr/bin/env python +# -*- coding: utf-8 + +import sys +import 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) + if res != 0: + print("VALIDATION FAILED") + else: + print("VALIDATED") + + +def main(argv): + schema_validate(argv[1], argv[2]) + + +if __name__ == '__main__': + main(sys.argv) diff --git a/NVIvanov/mephi4.py b/NVIvanov/mephi4.py new file mode 100644 index 0000000..e9ba8ab --- /dev/null +++ b/NVIvanov/mephi4.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import libxml2 +import optparse + + +def get_property_by_name(node, name): + for node_property in node.properties: + if node_property.type == 'attribute' and node_property.name == name: + return node_property.content + + +def 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_ERROR") + else: + print("VALIDATED") + parse_xml(xml_file) + + +def parse_xml(xml_file): + doc = libxml2.parseFile(xml_file) + mephi = doc.getRootElement() + faculty = mephi.children + while faculty is not None: + if faculty.type == "element": + title = faculty.children + while title is not None: + if title.type == "element": + print(title.content) + break + title = title.next + department = faculty.children + while department is not None: + if department.type == "element" and (department.name == "department" or department.name == "кафедра"): + number = department.children + while number is not None: + if number.type == "element": + print(number.content) + break + number = number.next + group = department.children + while group is not None: + if group.type == "element" and (group.name == "group" or group.name == "группа"): + title = group.children + while title is not None: + if title.type == "element": + print(title.content) + break + title = title.next + student = group.children + while student is not None: + if student.type == "element" and (student.name == "student" or student.name == "студент"): + print(get_property_by_name(student, "name")) + student = student.next + group = group.next + department = department.next + faculty = faculty.next + doc.freeDoc() + + +def get_option_parser(): + op = optparse.OptionParser(description=U"XPath запросы", + prog="xpath", version="0.1", usage=U"%prog") + op.add_option("-x", "--xml", dest="xml", help=U"XML документ", metavar="XML_FILE") + return op + + +def xpath(xml_file, str1): + result = xml_file.xpathEval(str1) + return result + + +def get_response_size(array): + return len(array) + + +def output_response(response): + if isinstance(response, list): + for item in response: + print(item.content) + else: + print(response) + + +def main(): + op = get_option_parser() + options, arguments = op.parse_args() + if options.xml: + doc = libxml2.parseFile(options.xml) + ctxt = doc.xpathNewContext() + output_response(xpath(doc, "//department/@number")) + output_response(xpath(doc, "//student[@entered > 2000]/@name")) + output_response(xpath(doc, "//department[./group/student/@name = 'Nikolay Ivanov']/@number")) + output_response(get_response_size(xpath(doc, "//group[./title = 'K05-361']/student"))) + output_response(get_response_size(xpath(doc, "//department[./number = '36']/group/student"))) + + maximum = 0 + minimum = 0 + students_min = 100 + students_max = 0 + for i in range(1, get_response_size(xpath(doc, "//department"))): + path = xpath(doc, "//department[%s]/group/student" % i) + if students_max < get_response_size(path): + students_max = get_response_size(path) + maximum = i + if students_min > get_response_size(path): + students_min = get_response_size(path) + minimum = i + output_response(xpath(doc, "//department[%s]/group/student" % minimum)) + output_response(xpath(doc, "//department[%s]/group/student" % maximum)) + + output_response(xpath(doc, "//faculty[./department[@number = '36']]/@name")) + + ctxt.xpathFreeContext() + doc.freeDoc() + else: + op.print_help() + + +if __name__ == '__main__': + main() diff --git a/NVIvanov/mephi5.py b/NVIvanov/mephi5.py new file mode 100644 index 0000000..f600539 --- /dev/null +++ b/NVIvanov/mephi5.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import libxml2 +import libxslt +import sys + + +def transform(xml_file, xsl_file, out_file): + xml_doc = libxml2.parseFile(xml_file) + xsl_doc = libxml2.parseFile(xsl_file) + xsl = libxslt.parseStylesheetDoc(xsl_doc) + out_doc = xsl.applyStylesheet(xml_doc, None) + print(out_doc) + xsl.saveResultToFilename(out_file, out_doc, 0) + xsl.freeStylesheet() + out_doc.freeDoc() + xml_doc.freeDoc() + + +def main(argv): + transform(argv[1], argv[2], argv[3]) + + +if __name__ == '__main__': + main(sys.argv) diff --git a/NVIvanov/xslt_task1.xsl b/NVIvanov/xslt_task1.xsl new file mode 100644 index 0000000..6587f54 --- /dev/null +++ b/NVIvanov/xslt_task1.xsl @@ -0,0 +1,12 @@ + + + + + +

+
+ + + +
+
\ No newline at end of file diff --git a/NVIvanov/xslt_task2.xsl b/NVIvanov/xslt_task2.xsl new file mode 100644 index 0000000..2c8be17 --- /dev/null +++ b/NVIvanov/xslt_task2.xsl @@ -0,0 +1,24 @@ + + + + + + + +

Студенты

+ + + + + + + + + + + +
ИмяГруппа
+ + +
+
\ No newline at end of file