This file is indexed.

/usr/share/qt5/doc/qtxmlpatterns/qtxmlpatterns-schema-example.html is in qtxmlpatterns5-doc-html 5.5.1-2build1.

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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- schema.qdoc -->
  <title>XML Schema Validation Example | Qt XML Patterns 5.5</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.5</li>
<li><a href="qtxmlpatterns-index.html">Qt XML Patterns</a></li>
<li>XML Schema Validation Example</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
    </ul>
    </div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<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>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">XML Schema Validation Example</h1>
<span class="subtitle"></span>
<!-- $$$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 id="introduction">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 id="code-walk-through">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">&gt;</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 <code>schemaSelected()</code> resp. <code>instanceSelected()</code> slot. Furthermore the signal-slot connections for the validation button and the instance editor are set up.</p>
<p>The call to <code>schemaSelected(0)</code> and <code>instanceSelected(0)</code> 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">&gt;</span>document());
    <span class="keyword">new</span> XmlSyntaxHighlighter(instanceEdit<span class="operator">-</span><span class="operator">&gt;</span>document());

    schemaSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Contact Schema&quot;</span>));
    schemaSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Recipe Schema&quot;</span>));
    schemaSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Order Schema&quot;</span>));

    instanceSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Valid Contact Instance&quot;</span>));
    instanceSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Invalid Contact Instance&quot;</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">&gt;</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 <code>schemaSelected()</code> 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">&gt;</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">&gt;</span>addItem(tr(<span class="string">&quot;Valid Contact Instance&quot;</span>));
        instanceSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Invalid Contact Instance&quot;</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">&gt;</span>addItem(tr(<span class="string">&quot;Valid Recipe Instance&quot;</span>));
        instanceSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Invalid Recipe Instance&quot;</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">&gt;</span>addItem(tr(<span class="string">&quot;Valid Order Instance&quot;</span>));
        instanceSelection<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Invalid Order Instance&quot;</span>));
    }
    textChanged();

    <span class="keyword">const</span> <span class="type">QString</span> fileName <span class="operator">=</span> <span class="type">QStringLiteral</span>(<span class="string">&quot;:/schema_&quot;</span>)
        <span class="operator">+</span> <span class="type">QString</span><span class="operator">::</span>number(index) <span class="operator">+</span> <span class="type">QStringLiteral</span>(<span class="string">&quot;.xsd&quot;</span>);
    <span class="type">QFile</span> schemaFile(fileName);
    <span class="keyword">if</span> (<span class="operator">!</span>schemaFile<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly)) {
        qWarning() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Cannot open&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type">QDir</span><span class="operator">::</span>toNativeSeparators(fileName)
            <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">':'</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> schemaFile<span class="operator">.</span>errorString();
        <span class="keyword">return</span>;
    }

    <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">&gt;</span>setPlainText(schemaText);

    validate();
}</pre>
<p>In the <code>instanceSelected()</code> 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="keyword">if</span> (index <span class="operator">&lt;</span> <span class="number">0</span>) {
        instanceEdit<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(<span class="type">QString</span>());
        <span class="keyword">return</span>;
    }
    <span class="keyword">const</span> <span class="type">QString</span> fileName <span class="operator">=</span> <span class="type">QStringLiteral</span>(<span class="string">&quot;:/instance_&quot;</span>)
        <span class="operator">+</span> <span class="type">QString</span><span class="operator">::</span>number(<span class="number">2</span> <span class="operator">*</span> schemaSelection<span class="operator">-</span><span class="operator">&gt;</span>currentIndex() <span class="operator">+</span> index)
        <span class="operator">+</span> <span class="type">QStringLiteral</span>(<span class="string">&quot;.xml&quot;</span>);
    <span class="type">QFile</span> instanceFile(fileName);
    <span class="keyword">if</span> (<span class="operator">!</span>instanceFile<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly)) {
        qWarning() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Cannot open&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type">QDir</span><span class="operator">::</span>toNativeSeparators(fileName)
            <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">':'</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> instanceFile<span class="operator">.</span>errorString();
        <span class="keyword">return</span>;
    }
    <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">&gt;</span>setPlainText(instanceText);

    validate();
}</pre>
<p>The <code>validate()</code> 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 <code>MessageHandler</code> 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">&amp;</span>description<span class="operator">,</span>
                               <span class="keyword">const</span> <span class="type">QUrl</span> <span class="operator">&amp;</span>identifier<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qsourcelocation.html">QSourceLocation</a></span> <span class="operator">&amp;</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 <code>false</code> and the error is flagged in <code>errorOccurred</code>. 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 <code>MessageHandler</code></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">&gt;</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">&gt;</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">&amp;</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">&gt;</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">&gt;</span>setText(tr(<span class="string">&quot;validation successful&quot;</span>));
    }

    <span class="keyword">const</span> <span class="type">QString</span> styleSheet <span class="operator">=</span> <span class="type">QString</span>(<span class="string">&quot;QLabel {background: %1; padding: 3px}&quot;</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">&gt;</span>setStyleSheet(styleSheet);
}</pre>
<p>Files:</p>
<ul>
<li><a href="qtxmlpatterns-schema-mainwindow-cpp.html">schema/mainwindow.cpp</a></li>
<li><a href="qtxmlpatterns-schema-mainwindow-h.html">schema/mainwindow.h</a></li>
<li><a href="qtxmlpatterns-schema-schema-ui.html">schema/schema.ui</a></li>
<li><a href="qtxmlpatterns-schema-schema-mobiles-ui.html">schema/schema_mobiles.ui</a></li>
<li><a href="qtxmlpatterns-schema-files-invalid-contact-xml.html">schema/files/invalid_contact.xml</a></li>
<li><a href="qtxmlpatterns-schema-files-invalid-order-xml.html">schema/files/invalid_order.xml</a></li>
<li><a href="qtxmlpatterns-schema-files-invalid-recipe-xml.html">schema/files/invalid_recipe.xml</a></li>
<li><a href="qtxmlpatterns-schema-files-valid-contact-xml.html">schema/files/valid_contact.xml</a></li>
<li><a href="qtxmlpatterns-schema-files-valid-order-xml.html">schema/files/valid_order.xml</a></li>
<li><a href="qtxmlpatterns-schema-files-valid-recipe-xml.html">schema/files/valid_recipe.xml</a></li>
<li><a href="qtxmlpatterns-schema-main-cpp.html">schema/main.cpp</a></li>
<li><a href="qtxmlpatterns-schema-schema-pro.html">schema/schema.pro</a></li>
<li><a href="qtxmlpatterns-schema-schema-qrc.html">schema/schema.qrc</a></li>
</ul>
</div>
<!-- @@@schema -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
   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>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>