def Test(tester): tester.startGroup('Exercise namespace nodes') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '//namespace::node()' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/namespace::node()> #expr._rel is <Step: namespace::node()> #expr._step is <Step: descendant-or-self::node()> tester.startTest(EXPR) actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//node()/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(6, len(actual)) tester.testDone() EXPR = '/*/namespace::node()|/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/child::*> tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(4, len(actual)) tester.testDone() return tester.groupDone()
def __init__(self,iptFile,ctFile,truncTime): # Read epidemic and truncate to truncTime self.infectives = [] self.labels = [] epiFile = open(iptFile,'r') for line in epiFile: toks = line.split() label = atoi(toks[0]) I = atof(toks[1]) N = atof(toks[2]) R = atof(toks[3]) if N <= truncTime: # Take individuals who have been notified by truncTime if R > truncTime: # If R > truncTime, set R = truncTime R = truncTime self.infectives.append(Infective(label,I,N,R)) self.labels.append(label) epiFile.close() # Read in XML conFile = Uri.OsPathToUri(ctFile) xmlSrc = DefaultFactory.fromUri(conFile,stripElements=[(EMPTY_NAMESPACE,'*',1)]) self.doc = NonvalidatingReader.parse(xmlSrc) # Remove from the contact DOM any contact info # for individuals that are not present in labels self.labels = set(self.labels) for contact in self.doc.documentElement.xpath(u'tc:contact',explicitNss={u'tc':u'tracedcontacts'}): contactLabel = atoi(contact.getAttributeNS(None,u'id')) if contactLabel not in self.labels: self.doc.documentElement.removeChild(contact)
def run(self, iSrc): """ Given an InputSource, reads the document, processing XLinks therein. Warning: The document will be modified in place. """ document = NonvalidatingReader.parse(iSrc) xlinks = document.xpath("/descendant-or-self::*[@xlink:type]", explicitNss={"xlink": XLINK_NAMESPACE}) for link in xlinks: xlink = XLinkElements.Create(link, iSrc) xlink.process() return document
def run(self, iSrc): """ Given an InputSource, reads the document, processing XLinks therein. Warning: The document will be modified in place. """ document = NonvalidatingReader.parse(iSrc) xlinks = document.xpath('/descendant-or-self::*[@xlink:type]', explicitNss={'xlink': XLINK_NAMESPACE}) for link in xlinks: xlink = XLinkElements.Create(link, iSrc) xlink.process() return document
def Test(tester): tester.startGroup('CDATA sections in doc') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '/doc/elem/text()' expr = Compile(EXPR) tester.startTest(EXPR) actual = [ node.data for node in expr.evaluate(con) ] tester.compare(actual, ["abc"]*3) tester.testDone() return tester.groupDone()
def Test(tester): tester.startGroup('CDATA sections in doc') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '/doc/elem/text()' expr = Compile(EXPR) tester.startTest(EXPR) actual = [node.data for node in expr.evaluate(con)] tester.compare(actual, ["abc"] * 3) tester.testDone() return tester.groupDone()
def loadFromFile(self, filename): #rngParser = libxml2.relaxNGNewMemParserCtxt(VALIDATION_SCHEMA, len(VALIDATION_SCHEMA)) #rngContext = rngParser.relaxNGParse().relaxNGNewValidCtxt() #file = fileRead(filename, 'r') #doc = libxml2.parseDoc(file) #if doc.relaxNGValidateDoc(rngContext) != 0: # raise InvalidLibraryException(filename) #for item in doc.xpathEval('/library/item'): # self.addItem({'authors': map(lambda x: x.content, item.xpathEval('authors/author')), # 'title': item.xpathEval('title')[0].content, # 'type': item.xpathEval('type')[0].content, # 'date': item.xpathEval('date')[0].content, # 'language': item.xpathEval('language')[0].content}) factory = InputSource.DefaultFactory schema = factory.fromString(VALIDATION_SCHEMA) validator = RelaxNgValidator(schema) file = Uri.OsPathToUri(filename, attemptAbsolute=1) # validate file if not(validator.isValid(factory.fromUri(file))): raise InvalidLibraryException(filename) doc = NonvalidatingReader.parse(factory.fromUri(file)) # read items from document for item in doc.xpath('/library/item'): self.addItem({'authors': map(lambda x: x.childNodes[0].nodeValue.strip(), item.xpath('authors/author')), 'title': item.xpath('title')[0].childNodes[0].nodeValue.strip(), 'type': item.xpath('type')[0].childNodes[0].nodeValue.strip(), 'date': item.xpath('date')[0].childNodes[0].nodeValue.strip(), 'language': item.xpath('language')[0].childNodes[0].nodeValue.strip()})