/usr/share/qt5/doc/qtxmlpatterns/qtxmlpatterns-xmlpatterns-schema-example.html is in qtxmlpatterns5-doc-html 5.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- schema.qdoc -->
<title>XML Schema Validation Example | QtXmlPatterns </title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.3</li>
<li><a href="qtxmlpatterns-index.html">Qt XML Patterns</a></li>
<li>XML Schema Validation Example</li>
<li id="buildversion">
Qt 5.3.2 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#introduction">Introduction</a></li>
<li class="level2"><a href="#the-user-interface">The User Interface</a></li>
<li class="level2"><a href="#validating-xml-instance-documents">Validating XML Instance Documents</a></li>
<li class="level1"><a href="#code-walk-through">Code Walk-Through</a></li>
<li class="level2"><a href="#the-ui-class-mainwindow">The UI Class: MainWindow</a></li>
</ul>
</div>
<h1 class="title">XML Schema Validation Example</h1>
<span class="subtitle"></span>
<!-- $$$xmlpatterns/schema-description -->
<div class="descr"> <a name="details"></a>
<p>The XML Schema Validation example shows how to use Qt XML Patterns to validate XML with a W3C XML Schema.</p>
<a name="introduction"></a>
<h2>Introduction</h2>
<p>The example application shows different XML schema definitions and for every definition two XML instance documents, one that is valid according to the schema and one that is not. The user can select the valid or invalid instance document, change it and validate it again.</p>
<a name="the-user-interface"></a>
<h3>The User Interface</h3>
<p>The UI for this example was created using Qt Designer:</p>
<p class="centerAlign"><img src="images/schema-example.png" alt="" /></p><p>The UI consists of three parts, at the top the XML schema selection and the schema viewer, below the XML instance selection and the instance editor and at the bottom the validation status label next to the validation button.</p>
<a name="validating-xml-instance-documents"></a>
<h3>Validating XML Instance Documents</h3>
<p>You can select one of the three predefined XML schemas and for each schema an valid or invalid instance document. A click on the 'Validate' button will validate the content of the XML instance editor against the schema from the XML schema viewer. As you can modify the content of the instance editor, different instances can be tested and validation error messages analysed.</p>
<a name="code-walk-through"></a>
<h2>Code Walk-Through</h2>
<p>The example's main() function creates the standard instance of QApplication. Then it creates an instance of the mainwindow class, shows it, and starts the Qt event loop:</p>
<pre class="cpp"><span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span><span class="operator">*</span> argv<span class="operator">[</span><span class="operator">]</span>)
{
Q_INIT_RESOURCE(schema);
<span class="type">QApplication</span> app(argc<span class="operator">,</span> argv);
MainWindow<span class="operator">*</span> <span class="keyword">const</span> window <span class="operator">=</span> <span class="keyword">new</span> MainWindow;
window<span class="operator">-</span><span class="operator">></span>show();
<span class="keyword">return</span> app<span class="operator">.</span>exec();
}</pre>
<a name="the-ui-class-mainwindow"></a>
<h3>The UI Class: MainWindow</h3>
<p>The example's UI is a conventional Qt GUI application inheriting QMainWindow and the class generated by Qt Designer:</p>
<pre class="cpp"><span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type">QMainWindow</span><span class="operator">,</span>
<span class="keyword">private</span> Ui<span class="operator">::</span>SchemaMainWindow
{
Q_OBJECT
<span class="keyword">public</span>:
MainWindow();
<span class="keyword">private</span> Q_SLOTS:
<span class="type">void</span> schemaSelected(<span class="type">int</span> index);
<span class="type">void</span> instanceSelected(<span class="type">int</span> index);
<span class="type">void</span> validate();
<span class="type">void</span> textChanged();
<span class="keyword">private</span>:
<span class="type">void</span> moveCursor(<span class="type">int</span> line<span class="operator">,</span> <span class="type">int</span> column);
};</pre>
<p>The constructor fills the schema and instance QComboBox selections with the predefined schemas and instances and connects their currentIndexChanged() signals to the window's <tt>schemaSelected()</tt> resp. <tt>instanceSelected()</tt> slot. Furthermore the signal-slot connections for the validation button and the instance editor are set up.</p>
<p>The call to <tt>schemaSelected(0)</tt> and <tt>instanceSelected(0)</tt> will trigger the validation of the initial Contact Schema example.</p>
<pre class="cpp">MainWindow<span class="operator">::</span>MainWindow()
{
setupUi(<span class="keyword">this</span>);
<span class="keyword">new</span> XmlSyntaxHighlighter(schemaView<span class="operator">-</span><span class="operator">></span>document());
<span class="keyword">new</span> XmlSyntaxHighlighter(instanceEdit<span class="operator">-</span><span class="operator">></span>document());
schemaSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Contact Schema"</span>));
schemaSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Recipe Schema"</span>));
schemaSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Order Schema"</span>));
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Valid Contact Instance"</span>));
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Invalid Contact Instance"</span>));
connect(schemaSelection<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> SLOT(schemaSelected(<span class="type">int</span>)));
connect(instanceSelection<span class="operator">,</span> SIGNAL(currentIndexChanged(<span class="type">int</span>))<span class="operator">,</span> SLOT(instanceSelected(<span class="type">int</span>)));
connect(validateButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> SLOT(validate()));
connect(instanceEdit<span class="operator">,</span> SIGNAL(textChanged())<span class="operator">,</span> SLOT(textChanged()));
validationStatus<span class="operator">-</span><span class="operator">></span>setAlignment(<span class="type">Qt</span><span class="operator">::</span>AlignCenter <span class="operator">|</span> <span class="type">Qt</span><span class="operator">::</span>AlignVCenter);
schemaSelected(<span class="number">0</span>);
instanceSelected(<span class="number">0</span>);
}</pre>
<p>In the <tt>schemaSelected()</tt> slot the content of the instance selection is adapted to the selected schema and the corresponding schema is loaded from the resource file and displayed in the schema viewer. At the end of the method a revalidation is triggered.</p>
<pre class="cpp"><span class="type">void</span> MainWindow<span class="operator">::</span>schemaSelected(<span class="type">int</span> index)
{
instanceSelection<span class="operator">-</span><span class="operator">></span>clear();
<span class="keyword">if</span> (index <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Valid Contact Instance"</span>));
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Invalid Contact Instance"</span>));
} <span class="keyword">else</span> <span class="keyword">if</span> (index <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Valid Recipe Instance"</span>));
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Invalid Recipe Instance"</span>));
} <span class="keyword">else</span> <span class="keyword">if</span> (index <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Valid Order Instance"</span>));
instanceSelection<span class="operator">-</span><span class="operator">></span>addItem(tr(<span class="string">"Invalid Order Instance"</span>));
}
textChanged();
<span class="type">QFile</span> schemaFile(<span class="type">QString</span>(<span class="string">":/schema_%1.xsd"</span>)<span class="operator">.</span>arg(index));
schemaFile<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly);
<span class="keyword">const</span> <span class="type">QString</span> schemaText(<span class="type">QString</span><span class="operator">::</span>fromUtf8(schemaFile<span class="operator">.</span>readAll()));
schemaView<span class="operator">-</span><span class="operator">></span>setPlainText(schemaText);
validate();
}</pre>
<p>In the <tt>instanceSelected()</tt> slot the selected instance is loaded from the resource file and loaded into the instance editor an the revalidation is triggered again.</p>
<pre class="cpp"><span class="type">void</span> MainWindow<span class="operator">::</span>instanceSelected(<span class="type">int</span> index)
{
<span class="type">QFile</span> instanceFile(<span class="type">QString</span>(<span class="string">":/instance_%1.xml"</span>)<span class="operator">.</span>arg((<span class="number">2</span><span class="operator">*</span>schemaSelection<span class="operator">-</span><span class="operator">></span>currentIndex()) <span class="operator">+</span> index));
instanceFile<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly);
<span class="keyword">const</span> <span class="type">QString</span> instanceText(<span class="type">QString</span><span class="operator">::</span>fromUtf8(instanceFile<span class="operator">.</span>readAll()));
instanceEdit<span class="operator">-</span><span class="operator">></span>setPlainText(instanceText);
validate();
}</pre>
<p>The <tt>validate()</tt> slot does the actual work in this example. At first it stores the content of the schema viewer and the editor into temporary variables. Then it instanciates a <tt>MessageHandler</tt> object which inherits from <a href="qabstractmessagehandler.html">QAbstractMessageHandler</a> and is a convenience class to store error messages from the XmlPatterns system.</p>
<pre class="cpp"><span class="keyword">class</span> MessageHandler : <span class="keyword">public</span> <span class="type"><a href="qabstractmessagehandler.html">QAbstractMessageHandler</a></span>
{
<span class="keyword">public</span>:
MessageHandler()
: <span class="type"><a href="qabstractmessagehandler.html">QAbstractMessageHandler</a></span>(<span class="number">0</span>)
{
}
<span class="type">QString</span> statusMessage() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_description;
}
<span class="type">int</span> line() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_sourceLocation<span class="operator">.</span>line();
}
<span class="type">int</span> column() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_sourceLocation<span class="operator">.</span>column();
}
<span class="keyword">protected</span>:
<span class="keyword">virtual</span> <span class="type">void</span> handleMessage(<span class="type">QtMsgType</span> type<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>description<span class="operator">,</span>
<span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&</span>identifier<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsourcelocation.html">QSourceLocation</a></span> <span class="operator">&</span>sourceLocation)
{
Q_UNUSED(type);
Q_UNUSED(identifier);
m_messageType <span class="operator">=</span> type;
m_description <span class="operator">=</span> description;
m_sourceLocation <span class="operator">=</span> sourceLocation;
}
<span class="keyword">private</span>:
<span class="type">QtMsgType</span> m_messageType;
<span class="type">QString</span> m_description;
<span class="type"><a href="qsourcelocation.html">QSourceLocation</a></span> m_sourceLocation;
};</pre>
<p>After the <a href="qxmlschema.html">QXmlSchema</a> is instanciated and the message handler set on it, the <a href="qxmlschema.html#load">load()</a> method is called with the schema data as argument. If the schema is invalid or a parsing error has occurred, <a href="qxmlschema.html#isValid">isValid()</a> returns <tt>false</tt> and the error is flagged in <tt>errorOccurred</tt>. If the loading was successful, a <a href="qxmlschemavalidator.html">QXmlSchemaValidator</a> is instanciated and the schema passed in the constructor. A call to <a href="qxmlschemavalidator.html#validate">validate()</a> will validate the passed XML instance data against the XML schema. The return value of that method signals whether the validation was successful. Depending on the success the status label is set to 'validation successful' or the error message stored in the <tt>MessageHandler</tt></p>
<p>The rest of the code does only some fancy coloring and eyecandy.</p>
<pre class="cpp"><span class="type">void</span> MainWindow<span class="operator">::</span>validate()
{
<span class="keyword">const</span> <span class="type">QByteArray</span> schemaData <span class="operator">=</span> schemaView<span class="operator">-</span><span class="operator">></span>toPlainText()<span class="operator">.</span>toUtf8();
<span class="keyword">const</span> <span class="type">QByteArray</span> instanceData <span class="operator">=</span> instanceEdit<span class="operator">-</span><span class="operator">></span>toPlainText()<span class="operator">.</span>toUtf8();
MessageHandler messageHandler;
<span class="type"><a href="qxmlschema.html">QXmlSchema</a></span> schema;
schema<span class="operator">.</span>setMessageHandler(<span class="operator">&</span>messageHandler);
schema<span class="operator">.</span>load(schemaData);
bool errorOccurred <span class="operator">=</span> <span class="keyword">false</span>;
<span class="keyword">if</span> (<span class="operator">!</span>schema<span class="operator">.</span>isValid()) {
errorOccurred <span class="operator">=</span> <span class="keyword">true</span>;
} <span class="keyword">else</span> {
<span class="type"><a href="qxmlschemavalidator.html">QXmlSchemaValidator</a></span> validator(schema);
<span class="keyword">if</span> (<span class="operator">!</span>validator<span class="operator">.</span>validate(instanceData))
errorOccurred <span class="operator">=</span> <span class="keyword">true</span>;
}
<span class="keyword">if</span> (errorOccurred) {
validationStatus<span class="operator">-</span><span class="operator">></span>setText(messageHandler<span class="operator">.</span>statusMessage());
moveCursor(messageHandler<span class="operator">.</span>line()<span class="operator">,</span> messageHandler<span class="operator">.</span>column());
} <span class="keyword">else</span> {
validationStatus<span class="operator">-</span><span class="operator">></span>setText(tr(<span class="string">"validation successful"</span>));
}
<span class="keyword">const</span> <span class="type">QString</span> styleSheet <span class="operator">=</span> <span class="type">QString</span>(<span class="string">"QLabel {background: %1; padding: 3px}"</span>)
<span class="operator">.</span>arg(errorOccurred <span class="operator">?</span> <span class="type">QColor</span>(<span class="type">Qt</span><span class="operator">::</span>red)<span class="operator">.</span>lighter(<span class="number">160</span>)<span class="operator">.</span>name() :
<span class="type">QColor</span>(<span class="type">Qt</span><span class="operator">::</span>green)<span class="operator">.</span>lighter(<span class="number">160</span>)<span class="operator">.</span>name());
validationStatus<span class="operator">-</span><span class="operator">></span>setStyleSheet(styleSheet);
}</pre>
<p>Files:</p>
<ul>
<li><a href="qtxmlpatterns-xmlpatterns-schema-mainwindow-cpp.html">xmlpatterns/schema/mainwindow.cpp</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-mainwindow-h.html">xmlpatterns/schema/mainwindow.h</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-schema-ui.html">xmlpatterns/schema/schema.ui</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-schema-mobiles-ui.html">xmlpatterns/schema/schema_mobiles.ui</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-invalid-contact-xml.html">xmlpatterns/schema/files/invalid_contact.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-invalid-order-xml.html">xmlpatterns/schema/files/invalid_order.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-invalid-recipe-xml.html">xmlpatterns/schema/files/invalid_recipe.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-valid-contact-xml.html">xmlpatterns/schema/files/valid_contact.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-valid-order-xml.html">xmlpatterns/schema/files/valid_order.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-files-valid-recipe-xml.html">xmlpatterns/schema/files/valid_recipe.xml</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-main-cpp.html">xmlpatterns/schema/main.cpp</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-schema-pro.html">xmlpatterns/schema/schema.pro</a></li>
<li><a href="qtxmlpatterns-xmlpatterns-schema-schema-qrc.html">xmlpatterns/schema/schema.qrc</a></li>
</ul>
</div>
<!-- @@@xmlpatterns/schema -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2014 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|