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

For xpath #25

Open
wants to merge 4 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
22 changes: 22 additions & 0 deletions mephi.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<university name="Mephi" metro="metro Kashirskoe" adress="Moscow,Kashirskoe shosse,31">
<faculty name="K">
<department name="Information tehnology">
<group name="361" entryyear="2012">
<student>Anisimova Natalia</student>
<student>Artemiev Dmitriy</student>
<student>Bubenko Kirill</student>
<student>Jelouhova Alena</student>
<student>Zamanov Aynur</student>
<student>Miheev Denis</student>
<student>Pivovarov Sasha</student>
<student>Samsonov Artem</student>
<student>Solovieva Anna</student>
<student>Sidorova Liubov</student>
<student>Tarmazakov Evgeniy</student>
<student>Titorenko Alexey</student>
<student>Shtaniko Sasha</student>
</group>
</department>
</faculty>
</university>
20 changes: 20 additions & 0 deletions programm_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env python
import sys, libxml2

def open(mephi_xml):
doc = libxml2.parseFile(mephi_xml)
doc.freeDoc()

def main(argv):
open(argv[1])
from xml.dom.minidom import *

xml = parse('mephi.xml')
name = xml.getElementsByTagName('student')

for node in name:
print node.childNodes[0].nodeValue


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

import optparse, libxml2

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 document", 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()
28 changes: 28 additions & 0 deletions xpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, libxml2

def open(xml_file, query):
doc = libxml2.parseFile (xml_file)
ctxt = doc.xpathNewContext()
students = ctxt.xpathEval(query)
print students
ctxt.xpathFreeContext()
doc.freeDoc()

def main(argv):
queries = ["string(/university/faculty[@name='K']/*/attribute::name)",
"//group[@entryyear>='2012']/*",
"string(//group[child::student='Sidorova Liubov']/attribute::name)",
"count(//group[@name='361'][@entryyear='2012']/*)",
"count(//department[@name='Information tehnology']/descendant::student)",
"string(//faculty[child::department[@name='361']]/attribute::name)",
"concat(string(//faculty/attribute::name),string(1+2*(2014.0-number(//group/attribute::entryyear))),'-',string(//group/attribute::name))"]
for s in queries:
print s
print
open(argv[1], s)
print

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