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 8.4 KiB

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