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

mephi.xml #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions NVIvanov/mephi.dtd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT mephi (faculty+)>
<!ATTLIST mephi name CDATA #REQUIRED>

<!ELEMENT faculty (department+)>
<!ATTLIST faculty
name CDATA #REQUIRED
short_name CDATA #IMPLIED>

<!ELEMENT department (group+)>
<!ATTLIST department number CDATA #REQUIRED>

<!ELEMENT group (student+)>
<!ATTLIST group name CDATA #REQUIRED>

<!ELEMENT student EMPTY>
<!ATTLIST student name CDATA #REQUIRED>
16 changes: 16 additions & 0 deletions NVIvanov/mephi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mephi SYSTEM "mephi.dtd">
<mephi name="НИЯУ МИФИ"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="mephi.xsd">
<faculty name="Кибернетики и информационной безопасности" short_name="КиБ">
<department number="36">
<group name="К05-361">
<student name="Чкадуа Антон Витальевич" entered="2013"/>
</group>
<group name="К05-362">
<student name="Иванов Иван Иванович" entered="2013"/>
</group>
</department>
</faculty>
</mephi>
68 changes: 68 additions & 0 deletions NVIvanov/mephi.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mephi" type="university"/>
<xs:element name="МИФИ" substitutionGroup="mephi"/>

<xs:element name="faculty" type="faculty"/>
<xs:element name="факультет" substitutionGroup="faculty"/>

<xs:complexType name="university">
<xs:all>
<xs:element ref="faculty"/>
</xs:all>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>

<xs:complexType name="faculty">
<xs:sequence>
<xs:element ref="department" maxOccurs="10"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="short_name" type="xs:string" use="optional"/>
</xs:complexType>

<xs:element name="department" type="department"/>
<xs:element name="кафедра" substitutionGroup="department"/>

<xs:complexType name="department">
<xs:sequence>
<xs:element ref="group" minOccurs="1" maxOccurs="100"/>
</xs:sequence>
<xs:attribute name="number" type="number"/>
</xs:complexType>

<xs:element name="number" type="number"/>
<xs:element name="номер" substitutionGroup="number"/>

<xs:simpleType name="number">
<xs:restriction base="xs:positiveInteger">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>

<xs:element name="group" type="group"/>
<xs:element name="группа" substitutionGroup="group"/>

<xs:complexType name="group">
<xs:sequence>
<xs:element minOccurs="1" ref="student"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>

<xs:element name="student" type="student"/>
<xs:element name="студент" substitutionGroup="student"/>

<xs:complexType name="student">
<xs:complexContent>
<xs:extension base="person">
<xs:attribute name = "entered" type="number" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="person">
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="born" type="xs:integer"/>
</xs:complexType>
</xs:schema>
35 changes: 35 additions & 0 deletions NVIvanov/mephi1.py
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions NVIvanov/mephi2.py
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 24 additions & 0 deletions NVIvanov/mephi3.py
Original file line number Diff line number Diff line change
@@ -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)
127 changes: 127 additions & 0 deletions NVIvanov/mephi4.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions NVIvanov/mephi5.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions NVIvanov/xslt_task1.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//faculty">
<p><xsl:value-of select="department/@number"/></p>
</xsl:for-each>
</body>
</html>

</xsl:template>
</xsl:stylesheet>
24 changes: 24 additions & 0 deletions NVIvanov/xslt_task2.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h2>Студенты</h2>
<table border="1">
<tr>
<th>Имя</th>
<th>Группа</th>
</tr>
<xsl:for-each select="//group">
<tr>
<td><xsl:value-of select="./student/@name"/></td>
<td><xsl:value-of select="./student/parent::group/@name"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>