You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlvalidate.html 9.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  18. <title>XMLValidate Task</title>
  19. </head>
  20. <body>
  21. <h2><a name="xmlvalidate">XMLValidate</a></h2>
  22. <h3>Description</h3>
  23. <p>This task checks that XML files are valid (or only well formed). The
  24. task uses the SAX2 parser implementation provided by JAXP by default
  25. (probably the one that is used by Ant itself), but one can specify any
  26. SAX1/2 parser if needed.</p>
  27. <p>This task supports the use of nested
  28. <li><a href="../CoreTypes/xmlcatalog.html"><tt>&lt;xmlcatalog&gt;</tt></a> elements</li>
  29. <li><tt>&lt;dtd&gt;</tt> elements which are used to resolve DTDs and entities</li>
  30. <li><tt>&lt;attribute&gt;</tt> elements which are used to set features on the parser.
  31. These can be any number of
  32. <a href="http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html#package_description"><tt>http://xml.org/sax/features/</tt></a>
  33. or other features that your parser may support.</li>
  34. <li><tt>&lt;property&gt;</tt> elements, containing string properties
  35. </p>
  36. <p><b>Warning</b> : JAXP creates by default a non namespace aware parser.
  37. The <tt>"http://xml.org/sax/features/namespaces"</tt> feature is set
  38. by default to <tt>false</tt> by the JAXP implementation used by ant. To validate
  39. a document containing namespaces,
  40. set the namespaces feature to <tt>true</tt> explicitly by nesting the following element:
  41. <pre>
  42. &lt;attribute name="http://xml.org/sax/features/namespaces" value="true"/&gt;
  43. </pre>
  44. If you are using for instance a <tt>xsi:noNamespaceSchemaLocation</tt> attribute in your XML files,
  45. you will need this namespace support feature.
  46. </p>
  47. <p>If you are using a parser not generated by JAXP, by using the <tt>classname</tt> attribute of xmlvalidate, this warning
  48. may not apply.</p>
  49. <h3>Parameters</h3>
  50. <table border="1" cellpadding="2" cellspacing="0">
  51. <tr>
  52. <td valign="top"><b>Attribute</b></td>
  53. <td valign="top"><b>Description</b></td>
  54. <td align="center" valign="top"><b>Required</b></td>
  55. </tr>
  56. <tr>
  57. <td valign="top">file</td>
  58. <td valign="top">the file(s) you want to check. (optionally can use an embedded fileset)</td>
  59. <td valign="top" align="center">No</td>
  60. </tr>
  61. <tr>
  62. <td valign="top">lenient</td>
  63. <td valign="top">
  64. if true, only check the XML document is well formed
  65. (ignored if the specified parser is a SAX1 parser)
  66. </td>
  67. <td valign="top" align="center">No</td>
  68. </tr>
  69. <tr>
  70. <td valign="top">classname</td>
  71. <td valign="top">the parser to use.</td>
  72. <td align="center" valign="top">No</td>
  73. </tr>
  74. <tr>
  75. <td valign="top">classpathref</td>
  76. <td valign="top">where to find the parser class. Optionally can use an embedded <tt>&lt;classpath&gt;</tt> element.</td>
  77. <td align="center" valign="top">No</td>
  78. </tr>
  79. <tr>
  80. <td valign="top">failonerror</td>
  81. <td valign="top">fails on a error if set to true (defaults to true).</td>
  82. <td align="center" valign="top">No</td>
  83. </tr>
  84. <tr>
  85. <td valign="top">warn</td>
  86. <td valign="top">log parser warn events.</td>
  87. <td align="center" valign="top">No</td>
  88. </tr>
  89. </table>
  90. <h3><a name="nested">Nested Elements</a></h3>
  91. <h4>dtd</h4>
  92. <p>
  93. <tt>&lt;dtd&gt;</tt> is used to specify different locations for DTD resolution.
  94. </p>
  95. <table border="1" cellpadding="2" cellspacing="0">
  96. <tr>
  97. <td width="12%" valign="top"><b>Attribute</b></td>
  98. <td width="78%" valign="top"><b>Description</b></td>
  99. <td width="10%" valign="top"><b>Required</b></td>
  100. </tr>
  101. <tr>
  102. <td valign="top">publicId</td>
  103. <td valign="top">Public ID of the DTD to resolve</td>
  104. <td align="center" valign="top">Yes</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">location</td>
  108. <td valign="top">Location of the DTD to use, which can be a file,
  109. a resource, or a URL</td>
  110. <td align="center" valign="top">Yes</td>
  111. </tr>
  112. </table>
  113. <h4>xmlcatalog</h4>
  114. <p>The <a href="../CoreTypes/xmlcatalog.html"><tt>&lt;xmlcatalog&gt;</tt></a>
  115. element is used to perform entity resolution.</p>
  116. <h4>attribute</h4>
  117. <p>The <tt>&lt;attribute&gt;</tt> element is used to set parser features.<br>
  118. Features usable with the xerces parser are defined here :
  119. <a href="http://xml.apache.org/xerces-j/features.html">Setting features</a><br>
  120. SAX features are defined here:
  121. <a href="http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html#package_description"><tt>http://xml.org/sax/features/</tt></a><br>
  122. </p>
  123. <table border="1" cellpadding="2" cellspacing="0">
  124. <tr>
  125. <td width="12%" valign="top"><b>Attribute</b></td>
  126. <td width="78%" valign="top"><b>Description</b></td>
  127. <td width="10%" valign="top"><b>Required</b></td>
  128. </tr>
  129. <tr>
  130. <td valign="top">name</td>
  131. <td valign="top">The name of the feature</td>
  132. <td align="center" valign="top">Yes</td>
  133. </tr>
  134. <tr>
  135. <td valign="top">value</td>
  136. <td valign="top">The boolean value of the feature</td>
  137. <td align="center" valign="top">Yes</td>
  138. </tr>
  139. </table>
  140. </p>
  141. <h4>property</h4>
  142. <p>The <tt>&lt;property&gt;</tt> element is used to set properties.
  143. These properties are defined here for the xerces XML parser implementation :
  144. <a href="http://xml.apache.org/xerces-j/properties.html">XML Parser properties</a>
  145. Properties can be used to set the schema used to validate the XML file.
  146. </p>
  147. <table border="1" cellpadding="2" cellspacing="0">
  148. <tr>
  149. <td width="12%" valign="top"><b>Attribute</b></td>
  150. <td width="78%" valign="top"><b>Description</b></td>
  151. <td width="10%" valign="top"><b>Required</b></td>
  152. </tr>
  153. <tr>
  154. <td valign="top">name</td>
  155. <td valign="top">The name of the feature</td>
  156. <td align="center" valign="top">Yes</td>
  157. </tr>
  158. <tr>
  159. <td valign="top">value</td>
  160. <td valign="top">The string value of the property</td>
  161. <td align="center" valign="top">Yes</td>
  162. </tr>
  163. </table>
  164. </p>
  165. <h3>Examples</h3>
  166. <pre>
  167. &lt;xmlvalidate file="toto.xml"/&gt;
  168. </pre>
  169. Validate toto.xml
  170. <pre>
  171. &lt;xmlvalidate failonerror="no" lenient="yes" warn="yes"
  172. classname="org.apache.xerces.parsers.SAXParser"&gt;
  173. classpath="lib/xerces.jar"&gt;
  174. &lt;fileset dir="src" includes="style/*.xsl"/&gt;
  175. &lt;/xmlvalidate&gt;
  176. </pre>
  177. Validate all .xsl files in src/style, but only warn if there is an error, rather than
  178. halt the build.
  179. <pre>
  180. &lt;xmlvalidate file="struts-config.xml" warn="false"&gt;
  181. &lt;dtd publicId="-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
  182. location="struts-config_1_0.dtd"/&gt;
  183. &lt;/xmlvalidate&gt;
  184. </pre>
  185. Validate a struts configuration, using a local copy of the DTD.
  186. <pre>
  187. &lt;xmlvalidate failonerror="no"&gt;
  188. &lt;fileset dir="${project.dir}" includes="**/*.xml"/&gt;
  189. &lt;xmlcatalog refid="mycatalog"/&gt;
  190. &lt;/xmlvalidate&gt;
  191. </pre>
  192. Scan all XML files in the project, using a predefined catalog to map URIs to local files.
  193. <pre>
  194. &lt;xmlvalidate failonerror="no"&gt;
  195. &lt;fileset dir="${project.dir}" includes="**/*.xml"/&gt;
  196. &lt;xmlcatalog&gt;
  197. &lt;dtd
  198. publicId=&quot;-//ArielPartners//DTD XML Article V1.0//EN&quot;
  199. location=&quot;com/arielpartners/knowledgebase/dtd/article.dtd&quot;/&gt;
  200. &lt;/xmlcatalog&gt;
  201. &lt;/xmlvalidate&gt;
  202. </pre>
  203. Scan all XML files in the project, using the catalog defined inline.
  204. <pre>
  205. &lt;xmlvalidate failonerror="yes" lenient="no" warn="yes"&gt;
  206. &lt;fileset dir="xml" includes="**/*.xml"/&gt;
  207. &lt;attribute name="http://xml.org/sax/features/validation" value="true"/&gt;
  208. &lt;attribute name="http://apache.org/xml/features/validation/schema" value="true"/&gt;
  209. &lt;attribute name="http://xml.org/sax/features/namespaces" value="true"/&gt;
  210. &lt;/xmlvalidate&gt;
  211. </pre>
  212. Validate all .xml files in xml directory with the parser configured to perform XSD validation.
  213. Note: The parser must support the feature
  214. <code>http://apache.org/xml/features/validation/schema</code>.
  215. The <a href="schemavalidate.html">schemavalidate</a> task is better for validating
  216. W3C XML Schemas, as it extends this task with the right options automatically enabled,
  217. and makes it easy to add a list of schema files/URLs to act as sources.
  218. <pre>
  219. <!-- Converts path to URL format -->
  220. &lt;pathconvert dirsep="/" property="xsd.file"&gt;
  221. &lt;path&gt;
  222. &lt;pathelement location="xml/doc.xsd"/&gt;
  223. &lt;/path&gt;
  224. &lt;/pathconvert&gt;
  225. &lt;xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false"
  226. failonerror="true" warn="true"&gt;
  227. &lt;attribute name="http://apache.org/xml/features/validation/schema"
  228. value="true"/&gt;
  229. &lt;attribute name="http://xml.org/sax/features/namespaces" value="true"/&gt;
  230. &lt;property
  231. name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
  232. value="${xsd.file}"/&gt;
  233. &lt;/xmlvalidate&gt;
  234. </pre>
  235. <br>
  236. Validate the file xml/endpiece-noSchema.xml against the schema xml/doc.xsd.
  237. <br>
  238. </body>
  239. </html>