Для работы с XML использую libxml2-2.7.8.
XMLSchema
Код:
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.company.ru/megaip"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.company.ru/megaip" elementFormDefault="qualified">
<complexType name="network">
<annotation>
<documentation>сетевые настройки</documentation>
</annotation>
<sequence>
<element name="address" minOccurs="1" maxOccurs="1"
type="tns:netparam">
<annotation>
<documentation>сетевой адрес</documentation>
</annotation>
</element>
<element name="netmask" type="tns:netparam" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>маска сети</documentation>
</annotation>
</element>
<element name="gate" type="tns:netparam" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>адрес шлюза</documentation>
</annotation>
</element>
</sequence>
</complexType>
<complexType name="image">
<annotation>
<documentation>настройки изображения</documentation>
</annotation>
<sequence>
<element name="width" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>ширина изображения</documentation>
</annotation>
</element>
<element name="height" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>высота изображения</documentation>
</annotation>
</element>
<element name="pos_x" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>позиция по оси X</documentation>
</annotation>
</element>
<element name="pos_y" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>позиция по оси Y</documentation>
</annotation>
</element>
<element name="scale" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>масштабирование</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
<enumeration value="2"></enumeration>
</restriction>
</simpleType>
</element>
<element name="row_scale" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
масштабированеи по строкам
</documentation>
</annotation>
</element>
<element name="column_scale" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
масштабирование по столбцам
</documentation>
</annotation>
</element>
</sequence>
</complexType>
<complexType name="color">
<annotation>
<documentation>настройки цвета</documentation>
</annotation>
<sequence>
<element name="depth" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>глубина цвета</documentation>
</annotation>
</element>
<element name="awb" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>
статус автоматического баланса белого
</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
</restriction>
</simpleType>
</element>
<element name="red_gain" minOccurs="1" maxOccurs="1" type="tns:gain">
<annotation>
<documentation>
аналоговое усиление красного
</documentation>
</annotation>
</element>
<element name="green_gain" type="tns:gain" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
аналоговое усиление зелёного
</documentation>
</annotation>
</element>
<element name="blue_gain" type="tns:gain" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
аналоговое усиление синего
</documentation>
</annotation>
</element>
</sequence>
</complexType>
<complexType name="exposure">
<annotation>
<documentation>настройки экспозиции</documentation>
</annotation>
<sequence>
<element name="mode" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>
ручная/автоматическая экспозиция
</documentation>
</annotation>
<simpleType>
<restriction base="string">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
</restriction>
</simpleType>
</element>
<element name="manual" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
ручная установка экспозиции
</documentation>
</annotation>
</element>
<element name="max" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
максимальное значение в автоматическом режиме
</documentation>
</annotation>
</element>
<element name="min" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
минимальное значение в автоматическом режиме
</documentation>
</annotation>
</element>
<element name="max_gain" type="tns:gain" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
максимальное аналоговое усиление в
автоматическом режиме
</documentation>
</annotation>
</element>
<element name="min_gain" type="tns:gain" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
минимальное аналоговое усиление в автоматическом
режиме
</documentation>
</annotation>
</element>
</sequence>
</complexType>
<complexType name="wdr">
<annotation>
<documentation>настройки WDR</documentation>
</annotation>
<sequence>
<element name="mode" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>режим работы WDR</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="1"></enumeration>
<enumeration value="2"></enumeration>
<enumeration value="3"></enumeration>
</restriction>
</simpleType>
</element>
<element name="point1" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>значение первой точки</documentation>
</annotation>
</element>
<element name="point2" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>значение второй точки</documentation>
</annotation>
</element>
<element name="point3" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>значение третье точки</documentation>
</annotation>
</element>
</sequence>
</complexType>
<complexType name="stream">
<annotation>
<documentation>настройки потока</documentation>
</annotation>
<sequence>
<element name="fps" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
количество кадров в секунду
</documentation>
</annotation>
</element>
<element name="format" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>
формат выходного потока
</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
<enumeration value="2"></enumeration>
<enumeration value="3"></enumeration>
<enumeration value="4"></enumeration>
<enumeration value="5"></enumeration>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<complexType name="special">
<annotation>
<documentation>специальные настройки</documentation>
</annotation>
<sequence>
<element name="ard" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>состояние АРД</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
</restriction>
</simpleType>
</element>
<element name="ard_value" type="int" minOccurs="1"
maxOccurs="1">
<annotation>
<documentation>
коэффициет заполнения для АРД
</documentation>
</annotation>
</element>
<element name="sync" minOccurs="1" maxOccurs="1">
<annotation>
<documentation>
режим внешней синхронизации матрицы
</documentation>
</annotation>
<simpleType>
<restriction base="int">
<enumeration value="0"></enumeration>
<enumeration value="1"></enumeration>
<enumeration value="2"></enumeration>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<complexType name="root">
<all>
<element name="network" type="tns:network" minOccurs="1"
maxOccurs="1">
</element>
<element name="image" type="tns:image" minOccurs="1"
maxOccurs="1">
</element>
<element name="color" type="tns:color" minOccurs="1"
maxOccurs="1">
</element>
<element name="exposure" type="tns:exposure" minOccurs="1"
maxOccurs="1">
</element>
<element name="wdr" type="tns:wdr" minOccurs="1"
maxOccurs="1">
</element>
<element name="stream" type="tns:stream" minOccurs="1"
maxOccurs="1">
</element>
<element name="special" type="tns:special" minOccurs="1"
maxOccurs="1">
</element>
</all>
</complexType>
<simpleType name="netparam">
<annotation>
<documentation>сетевые параметры</documentation>
</annotation>
<restriction base="string">
<minLength value="7"></minLength>
<maxLength value="15"></maxLength>
</restriction>
</simpleType>
<element name="root" type="tns:root"></element>
<simpleType name="gain">
<restriction base="int">
<minInclusive value="0"></minInclusive>
<maxInclusive value="255"></maxInclusive>
</restriction>
</simpleType>
</schema>
XML
Код:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.company.ru/megaip">
<network>
<address>192.168.15.132</address>
<netmask>255.255.255.224</netmask>
<gate>192.168.15.129</gate>
</network>
<image>
<width>800</width>
<height>600</height>
<pos_x>0</pos_x>
<pos_y>0</pos_y>
<scale>0</scale>
<row_scale>0</row_scale>
<column_scale>0</column_scale>
</image>
<color>
<depth>16</depth>
<awb>0</awb>
<red_gain>127</red_gain>
<green_gain>127</green_gain>
<blue_gain>127</blue_gain>
</color>
<exposure>
<mode>0</mode>
<manual>0</manual>
<max>0</max>
<min>0</min>
<max_gain>0</max_gain>
<min_gain>0</min_gain>
</exposure>
<wdr>
<mode>1</mode>
<point1>0</point1>
<point2>0</point2>
<point3>0</point3>
</wdr>
<stream>
<fps>25</fps>
<format>4</format>
</stream>
<special>
<ard>0</ard>
<ard_value>0</ard_value>
<sync>0</sync>
</special>
</root>
Генерирование XML-файла:
Код: Выделить всё
#define XML_NAME_SPACE "http://www.company.ru/megaip"
#define XML_FILE_ROOT "root"
....................................
xmlDocPtr doc = NULL;
xmlNodePtr root = NULL, node = NULL;
xmlNsPtr ns = NULL;
char val[10];
int res;
if (NULL == (doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION)))
return XML_RET_ERROR;
if (NULL == (root = xmlNewNode(NULL, BAD_CAST XML_FILE_ROOT)))
return XML_RET_ERROR;
if (NULL == (ns = xmlNewNs(root, BAD_CAST XML_NAME_SPACE, NULL)))
return XML_RET_ERROR;
if (xmlDocSetRootElement(doc, root) != NULL)
return XML_RET_ERROR;
Провожу проверку с помощью xmllint:
Код: Выделить всё
$ xmllint --noout --nonet --schema config.xsd config.xml
config.xml validates
Насколько я понимаю, проверка прошла нормально. Теперь необходимо проводить подобную проверку в программе работающей с файлом. Добавил в программу код для проверки, как базу использовал http://wiki.njh.eu/XML-Schema_validation_with_libxml2
Увы, при запуске теста появляется сообщение element root: Schemas validity error : Element 'root': No matching global declaration available for the validation root.. Проверка не прошла.
Возникают вопросы:
- xmllint использует другой алгоритм?
- XMLSchema составлена не корректно?
- файл составлен не корректно?
Спасибо.