source: TI02-CSML/tags/stable-TI02-CSML-v0-1/parser/test.py @ 742

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/tags/stable-TI02-CSML-v0-1/parser/test.py
Revision 742, 2.5 KB checked in by domlowe, 16 years ago (diff)

Parser v0.1 commit, added comments to test docs

Line 
1"""Test for GML/CSML parsing code
2"""
3
4from cElementTree import *
5from Parser import *
6from xml.dom.minidom import parseString
7import elementtree.ElementTree as etree
8import sys
9
10
11#this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
12etree._namespace_map.update({
13    'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'})
14
15
16def removeInlineNS(csmlstring):
17    #  removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element.
18   
19    #remove all
20    csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','')
21    csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','')
22    csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','')
23    csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','')
24    csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','')
25   
26    #remove cmsl: prefixes
27    csmlstring=csmlstring.replace('<csml:','<')
28    csmlstring=csmlstring.replace('</csml:','</')
29    #add namespace declarations at top of document, after gml namespace declaration
30    csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml">', 'xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd">')
31   
32    return csmlstring
33
34
35
36#Parsing starts here:
37
38#######################################################
39# ElementTree-based parser test ROUND TRIP
40#######################################################
41# 1. Test parsing from CSML file
42tree = ElementTree(file='example.xml')
43dataset = Dataset()
44
45#Calling the fromXML method reads the CSML into memory.
46dataset.fromXML(tree.getroot())
47
48#Reading some values from memory and printing it out::
49print dataset.featureCollection.members[3].profileSeriesDomain.domainReference.times.timePositions
50
51#This creates a new CSML document string from the CSML objects in memory.
52#Hopefully the CSML output should be the same as the CSML it read in.
53csml = dataset.toXML()
54
55#Tidy up and print the CSML document:
56strCSML= parseString(tostring(csml)).toprettyxml()
57strCSML=removeInlineNS(strCSML)
58print strCSML
59
Note: See TracBrowser for help on using the repository browser.