RDK Documentation (Open Sourced RDK Components)
validateDataModel.py
1 ##########################################################################
2 # If not stated otherwise in this file or this component's LICENSE
3 # file the following copyright and licenses apply:
4 #
5 # Copyright 2016 RDK Management
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ##########################################################################
19 
20 # Python script to validate xml file with its corresponsding xsd schema
21 
22 import sys
23 from lxml import etree
24 
25 # Python-lxml utility class for validating xml file with xsd schema
26 class Validator:
27  def __init__(self,xsd_path):
28  xmlschema_doc = etree.parse(xsd_path)
29  self.xmlschema = etree.XMLSchema(xmlschema_doc)
30 
31  def validate(self, xml_path) :
32  xml_doc = etree.parse(xml_path)
33  result = self.xmlschema.validate(xml_doc)
34  if not result:
35  for error in self.xmlschema.error_log:
36  print (error.message, error.line, error.column)
37  return result
38 
39 
40 # Validate xml based on cwmp xsd file
41 if len (sys.argv) != 3 :
42  print ('Usage: python validateDataModel.py <XML_FILE_PATH> <XSD_FILE_PATH>')
43  raise Exception ('Invalid arguments to validateDataModel.py')
44 else:
45  xml_file_path=sys.argv[1]
46  xsd_file_path=sys.argv[2]
47  validator = Validator(xsd_file_path)
48 
49 if validator.validate(xml_file_path):
50  print('Data-model is valid')
51 else:
52  print('data-model.xml is not valid');
53  raise Exception('data-model.xml is not matching with the cwmp schema')
validateDataModel.Validator
Definition: validateDataModel.py:26
validateDataModel.Validator.xmlschema
xmlschema
Definition: validateDataModel.py:29
Exception
Definition: Exception.hpp:42