SchemaValidate

Description

This task validates XML files described by an XML Schema. The task extends the XmlValidate task with XSD-specific features.

  1. The parser is created validating and namespace aware
  2. Validation is turned on.
  3. and Schema validation is turned on.
  4. Any default schema supplied is used as the no-namespace schema
  5. All nested schema declarations are turned into the list of namespace-url bindings for schema lookup.
Note that nested catalogs are still used for lookup of the URLs given as the sources of schema documents, so you can still delegate lookup to a catalog, you just need to list all schema URIs and their URL equivalents.

This task supports the use of nested

  • <xmlcatalog> elements
  • <schema> elements, that bind a namespace URI to a URL or a local filename.
  • <dtd> elements which are used to resolve DTDs and entities.
  • <attribute> elements which are used to set features on the parser. These can be any number of http://xml.org/sax/features/ or other features that your parser may support.
  • <property> elements, containing string properties

    The task only supports SAX2 or later parsers: it is an error to specify a SAX1 parser.

    Parameters

    Attribute Description Required
    file the file(s) you want to check. (optionally can use an embedded fileset) No
    defaultSchemaFile filename of a no-namespace XSD file to provide the schema for no-namespace XML content. No
    noNamespaceURL URL of a no-namespace XSD file to provide the schema for no-namespace XML content. No
    noNamespaceFile filename of a no-namespace XSD file to provide the schema for no-namespace XML content. No
    fullchecking enable full schema checking. Slow but strict. No - default true
    lenient if true, only check the XML document is well formed No
    classname the parser to use. No
    classpathref where to find the parser class. Optionally can use an embedded <classpath> element. No
    failonerror fails on a error if set to true (defaults to true). No
    warn log parser warn events. No

    Nested Elements

    schema

    Identify the name and location of a schema that may be used in validating the document(s).

    Attribute Description Required
    namespace URI of the schema namespace Yes
    url URL of the schema One of url or file is required
    file file of the schema One of url or file is required

    dtd

    <dtd> is used to specify different locations for DTD resolution.

    Attribute Description Required
    publicId Public ID of the DTD to resolve Yes
    location Location of the DTD to use, which can be a file, a resource, or a URL Yes

    xmlcatalog

    The <xmlcatalog> element is used to perform entity resolution.

    attribute

    The <attribute> element is used to set parser features.
    Features usable with the xerces parser are defined here : Setting features
    SAX features are defined here: http://xml.org/sax/features/

    Attribute Description Required
    name The name of the feature Yes
    value The boolean value of the feature Yes

    property

    The <property> element is used to set properties. These properties are defined here for the xerces XML parser implementation : XML Parser properties Properties can be used to set the schema used to validate the XML file.

    Attribute Description Required
    name The name of the feature Yes
    value The string value of the property Yes

    Examples

    <xmlvalidate file="toto.xml"/>
    
    Validate toto.xml
    <xmlvalidate failonerror="no" lenient="yes" warn="yes"
                 classname="org.apache.xerces.parsers.SAXParser">
                 classpath="lib/xerces.jar">
      <fileset dir="src" includes="style/*.xsl"/>
    </xmlvalidate>
    
    Validate all .xsl files in src/style, but only warn if there is an error, rather than halt the build.
    
    <xmlvalidate file="struts-config.xml" warn="false">
      <dtd publicId="-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
           location="struts-config_1_0.dtd"/>
    </xmlvalidate>
    
    Validate a struts configuration, using a local copy of the DTD.
     
    <xmlvalidate failonerror="no">
      <fileset dir="${project.dir}" includes="**/*.xml"/>
      <xmlcatalog refid="mycatalog"/>
    </xmlvalidate>
    
    Scan all XML files in the project, using a predefined catalog to map URIs to local files.
    <xmlvalidate failonerror="no">
      <fileset dir="${project.dir}" includes="**/*.xml"/>
      <xmlcatalog>
           <dtd
             publicId="-//ArielPartners//DTD XML Article V1.0//EN"
             location="com/arielpartners/knowledgebase/dtd/article.dtd"/>
      </xmlcatalog>
    </xmlvalidate>
    
    Scan all XML files in the project, using the catalog defined inline.
    <xmlvalidate failonerror="yes" lenient="no" warn="yes">
      <fileset dir="xml" includes="**/*.xml"/>
      <attribute name="http://xml.org/sax/features/validation" value="true"/>
      <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
      <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
    </xmlvalidate>
    
    Validate all .xml files in xml directory with the parser configured to perform schema validation. Note: The parser must support the
    http://apache.org/xml/features/validation/schema
    feature.
    
    <pathconvert dirsep="/" property="xsd.file">
    <path>
       <pathelement location="xml/doc.xsd"/>
    </path>
    </pathconvert>
    
    <xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false"
      failonerror="true" warn="true">
      <attribute name="http://apache.org/xml/features/validation/schema"
      value="true"/>
      <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
      <property
      name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
      value="${xsd.file}"/>
    </xmlvalidate>
    

    Validate the file xml/endpiece-noSchema.xml against the schema xml/doc.xsd.

    Copyright © 2001-2002,2004-2005 The Apache Software Foundation. All rights Reserved.