~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/xmlpatterns-schema.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- schema.qdoc -->
 
6
<head>
 
7
  <title>Qt 4.6: XML Schema Validation Example</title>
 
8
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
9
</head>
 
10
<body>
 
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
12
<tr>
 
13
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
14
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
15
</tr></table><h1 class="title">XML Schema Validation Example<br /><span class="subtitle"></span>
 
16
</h1>
 
17
<p>Files:</p>
 
18
<ul>
 
19
<li><a href="xmlpatterns-schema-mainwindow-cpp.html">xmlpatterns/schema/mainwindow.cpp</a></li>
 
20
<li><a href="xmlpatterns-schema-mainwindow-h.html">xmlpatterns/schema/mainwindow.h</a></li>
 
21
<li><a href="xmlpatterns-schema-schema-ui.html">xmlpatterns/schema/schema.ui</a></li>
 
22
<li><a href="xmlpatterns-schema-files-invalid-contact-xml.html">xmlpatterns/schema/files/invalid_contact.xml</a></li>
 
23
<li><a href="xmlpatterns-schema-files-invalid-order-xml.html">xmlpatterns/schema/files/invalid_order.xml</a></li>
 
24
<li><a href="xmlpatterns-schema-files-invalid-recipe-xml.html">xmlpatterns/schema/files/invalid_recipe.xml</a></li>
 
25
<li><a href="xmlpatterns-schema-files-valid-contact-xml.html">xmlpatterns/schema/files/valid_contact.xml</a></li>
 
26
<li><a href="xmlpatterns-schema-files-valid-order-xml.html">xmlpatterns/schema/files/valid_order.xml</a></li>
 
27
<li><a href="xmlpatterns-schema-files-valid-recipe-xml.html">xmlpatterns/schema/files/valid_recipe.xml</a></li>
 
28
<li><a href="xmlpatterns-schema-main-cpp.html">xmlpatterns/schema/main.cpp</a></li>
 
29
<li><a href="xmlpatterns-schema-schema-pro.html">xmlpatterns/schema/schema.pro</a></li>
 
30
<li><a href="xmlpatterns-schema-schema-qrc.html">xmlpatterns/schema/schema.qrc</a></li>
 
31
</ul>
 
32
<p>This example shows how to use <a href="qtxmlpatterns.html">QtXmlPatterns</a> to validate XML with a W3C XML Schema.</p>
 
33
<ul><li><a href="#introduction">Introduction</a></li>
 
34
<ul><li><a href="#the-user-interface">The User Interface</a></li>
 
35
<li><a href="#validating-xml-instance-documents">Validating XML Instance Documents</a></li>
 
36
</ul>
 
37
<li><a href="#code-walk-through">Code Walk-Through</a></li>
 
38
<ul><li><a href="#the-ui-class-mainwindow">The UI Class: MainWindow</a></li>
 
39
</ul>
 
40
</ul>
 
41
<a name="introduction"></a>
 
42
<h2>Introduction</h2>
 
43
<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>
 
44
<a name="the-user-interface"></a>
 
45
<h3>The User Interface</h3>
 
46
<p>The UI for this example was created using <a href="designer-manual.html">Qt Designer</a>:</p>
 
47
<p align="center"><img src="images/schema-example.png" /></p><p>The UI consists of three parts, at the top the XML schema <a href="qcombobox.html">selection</a> and the schema <a href="qtextbrowser.html">viewer</a>, below the XML instance <a href="qcombobox.html">selection</a> and the instance <a href="qtextedit.html">editor</a> and at the bottom the validation status <a href="qlabel.html">label</a> next to the validation <a href="qpushbutton.html">button</a>.</p>
 
48
<a name="validating-xml-instance-documents"></a>
 
49
<h3>Validating XML Instance Documents</h3>
 
50
<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>
 
51
<a name="code-walk-through"></a>
 
52
<h2>Code Walk-Through</h2>
 
53
<p>The example's main() function creates the standard instance of <a href="qapplication.html">QApplication</a>. Then it creates an instance of the mainwindow class, shows it, and starts the Qt event loop:</p>
 
54
<pre> int main(int argc, char* argv[])
 
55
 {
 
56
     Q_INIT_RESOURCE(schema);
 
57
     QApplication app(argc, argv);
 
58
     MainWindow* const window = new MainWindow;
 
59
     window-&gt;show();
 
60
     return app.exec();
 
61
 }</pre>
 
62
<a name="the-ui-class-mainwindow"></a>
 
63
<h3>The UI Class: MainWindow</h3>
 
64
<p>The example's UI is a conventional Qt GUI application inheriting <a href="qmainwindow.html">QMainWindow</a> and the class generated by <a href="designer-manual.html">Qt Designer</a>:</p>
 
65
<pre> class MainWindow : public QMainWindow,
 
66
                    private Ui::SchemaMainWindow
 
67
 {
 
68
     Q_OBJECT
 
69
 
 
70
   public:
 
71
     MainWindow();
 
72
 
 
73
   private Q_SLOTS:
 
74
     void schemaSelected(int index);
 
75
     void instanceSelected(int index);
 
76
     void validate();
 
77
     void textChanged();
 
78
 
 
79
   private:
 
80
     void moveCursor(int line, int column);
 
81
 };</pre>
 
82
<p>The constructor fills the schema and instance <a href="qcombobox.html">QComboBox</a> selections with the predefined schemas and instances and connects their <a href="qcombobox.html#currentIndexChanged">currentIndexChanged()</a> signals to the window's <tt>schemaSelected()</tt> resp. <tt>instanceSelected()</tt> slot. Furthermore the signal-slot connections for the validation <a href="qpushbutton.html">button</a> and the instance <a href="qtextedit.html">editor</a> are set up.</p>
 
83
<p>The call to <tt>schemaSelected(0)</tt> and <tt>instanceSelected(0)</tt> will trigger the validation of the initial Contact Schema example.</p>
 
84
<pre> MainWindow::MainWindow()
 
85
 {
 
86
     setupUi(this);
 
87
 
 
88
     new XmlSyntaxHighlighter(schemaView-&gt;document());
 
89
     new XmlSyntaxHighlighter(instanceEdit-&gt;document());
 
90
 
 
91
     schemaSelection-&gt;addItem(tr(&quot;Contact Schema&quot;));
 
92
     schemaSelection-&gt;addItem(tr(&quot;Recipe Schema&quot;));
 
93
     schemaSelection-&gt;addItem(tr(&quot;Order Schema&quot;));
 
94
 
 
95
     instanceSelection-&gt;addItem(tr(&quot;Valid Contact Instance&quot;));
 
96
     instanceSelection-&gt;addItem(tr(&quot;Invalid Contact Instance&quot;));
 
97
 
 
98
     connect(schemaSelection, SIGNAL(currentIndexChanged(int)), SLOT(schemaSelected(int)));
 
99
     connect(instanceSelection, SIGNAL(currentIndexChanged(int)), SLOT(instanceSelected(int)));
 
100
     connect(validateButton, SIGNAL(clicked()), SLOT(validate()));
 
101
     connect(instanceEdit, SIGNAL(textChanged()), SLOT(textChanged()));
 
102
 
 
103
     validationStatus-&gt;setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
 
104
 
 
105
     schemaSelected(0);
 
106
     instanceSelected(0);
 
107
 }</pre>
 
108
<p>In the <tt>schemaSelected()</tt> slot the content of the instance <a href="qcombobox.html">selection</a> is adapted to the selected schema and the corresponding schema is loaded from the <a href="resources.html">resource file</a> and displayed in the schema <a href="qtextbrowser.html">viewer</a>. At the end of the method a revalidation is triggered.</p>
 
109
<pre> void MainWindow::schemaSelected(int index)
 
110
 {
 
111
     instanceSelection-&gt;clear();
 
112
     if (index == 0) {
 
113
         instanceSelection-&gt;addItem(tr(&quot;Valid Contact Instance&quot;));
 
114
         instanceSelection-&gt;addItem(tr(&quot;Invalid Contact Instance&quot;));
 
115
     } else if (index == 1) {
 
116
         instanceSelection-&gt;addItem(tr(&quot;Valid Recipe Instance&quot;));
 
117
         instanceSelection-&gt;addItem(tr(&quot;Invalid Recipe Instance&quot;));
 
118
     } else if (index == 2) {
 
119
         instanceSelection-&gt;addItem(tr(&quot;Valid Order Instance&quot;));
 
120
         instanceSelection-&gt;addItem(tr(&quot;Invalid Order Instance&quot;));
 
121
     }
 
122
     textChanged();
 
123
 
 
124
     QFile schemaFile(QString(&quot;:/schema_%1.xsd&quot;).arg(index));
 
125
     schemaFile.open(QIODevice::ReadOnly);
 
126
     const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
 
127
     schemaView-&gt;setPlainText(schemaText);
 
128
 
 
129
     validate();
 
130
 }</pre>
 
131
<p>In the <tt>instanceSelected()</tt> slot the selected instance is loaded from the <a href="resources.html">resource file</a> and loaded into the instance <a href="qtextedit.html">editor</a> an the revalidation is triggered again.</p>
 
132
<pre> void MainWindow::instanceSelected(int index)
 
133
 {
 
134
     QFile instanceFile(QString(&quot;:/instance_%1.xml&quot;).arg((2*schemaSelection-&gt;currentIndex()) + index));
 
135
     instanceFile.open(QIODevice::ReadOnly);
 
136
     const QString instanceText(QString::fromUtf8(instanceFile.readAll()));
 
137
     instanceEdit-&gt;setPlainText(instanceText);
 
138
 
 
139
     validate();
 
140
 }</pre>
 
141
<p>The <tt>validate()</tt> slot does the actual work in this example. At first it stores the content of the schema <a href="qtextbrowser.html">viewer</a> and the <a href="qtextedit.html">editor</a> into temporary <a href="qbytearray.html">variables</a>. 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>
 
142
<pre> class MessageHandler : public QAbstractMessageHandler
 
143
 {
 
144
     public:
 
145
         MessageHandler()
 
146
             : QAbstractMessageHandler(0)
 
147
         {
 
148
         }
 
149
 
 
150
         QString statusMessage() const
 
151
         {
 
152
             return m_description;
 
153
         }
 
154
 
 
155
         int line() const
 
156
         {
 
157
             return m_sourceLocation.line();
 
158
         }
 
159
 
 
160
         int column() const
 
161
         {
 
162
             return m_sourceLocation.column();
 
163
         }
 
164
 
 
165
     protected:
 
166
         virtual void handleMessage(QtMsgType type, const QString &amp;description,
 
167
                                    const QUrl &amp;identifier, const QSourceLocation &amp;sourceLocation)
 
168
         {
 
169
             Q_UNUSED(type);
 
170
             Q_UNUSED(identifier);
 
171
 
 
172
             m_messageType = type;
 
173
             m_description = description;
 
174
             m_sourceLocation = sourceLocation;
 
175
         }
 
176
 
 
177
     private:
 
178
         QtMsgType m_messageType;
 
179
         QString m_description;
 
180
         QSourceLocation m_sourceLocation;
 
181
 };</pre>
 
182
<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 occured, <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 <a href="qlabel.html">label</a> is set to 'validation successful' or the error message stored in the <tt>MessageHandler</tt></p>
 
183
<p>The rest of the code does only some fancy coloring and eyecandy.</p>
 
184
<pre> void MainWindow::validate()
 
185
 {
 
186
     const QByteArray schemaData = schemaView-&gt;toPlainText().toUtf8();
 
187
     const QByteArray instanceData = instanceEdit-&gt;toPlainText().toUtf8();
 
188
 
 
189
     MessageHandler messageHandler;
 
190
 
 
191
     QXmlSchema schema;
 
192
     schema.setMessageHandler(&amp;messageHandler);
 
193
 
 
194
     schema.load(schemaData);
 
195
 
 
196
     bool errorOccurred = false;
 
197
     if (!schema.isValid()) {
 
198
         errorOccurred = true;
 
199
     } else {
 
200
         QXmlSchemaValidator validator(schema);
 
201
         if (!validator.validate(instanceData))
 
202
             errorOccurred = true;
 
203
     }
 
204
 
 
205
     if (errorOccurred) {
 
206
         validationStatus-&gt;setText(messageHandler.statusMessage());
 
207
         moveCursor(messageHandler.line(), messageHandler.column());
 
208
     } else {
 
209
         validationStatus-&gt;setText(tr(&quot;validation successful&quot;));
 
210
     }
 
211
 
 
212
     const QString styleSheet = QString(&quot;QLabel {background: %1; padding: 3px}&quot;)
 
213
                                       .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() :
 
214
                                                            QColor(Qt::green).lighter(160).name());
 
215
     validationStatus-&gt;setStyleSheet(styleSheet);
 
216
 }</pre>
 
217
<p /><address><hr /><div align="center">
 
218
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
219
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
220
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
221
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
222
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
223
</html>