Volume 3, Issue 5 - September/October 2003
 
   
 

In this monthly column, an industry expert will answer common questions about VoiceXML and related technologies. Readers are encouraged to submit questions about VoiceXML, including development, voice-user interface design, and speech technology in general, or how VoiceXML is being used commercially in the marketplace. If you have a question about VoiceXML, e-mail it to and be sure to read future issues of VoiceXML Review for the answer.

Continued from previous page...

Q: Basic XML syntax checking is one thing, but how do I validate my documents against the VoiceXML 2.0 DTD?

A: To associate an XML document with a specific DTD, you can add a DOCTYPE declaration.

The following VoiceXML document includes a reference to the official VoiceXML 2.0 DTD as specified in Appendix B of the VoiceXML 2.0 specification (http://www.w3.org/TR/voicexml20/#dmlADTD):

http://www.w3.org/TR/voicexml20/vxml.dtd" []>
http://www.w3.org/2001/vxml" version="2.0">
   


Those familiar with VoiceXML will recognize that "hello" is not a valid VoiceXML tag, and any XML tool that performs DTD validation will detect and report this error.

If you're using Expat, you've probably noticed that it doesn't support DTD validation. For DTD validation in an open source, native C implementation, take a look at the GNOME project's XML parser, libxml2. (http://www.xmlsoft.org/). If you download and install the appropriate package for your particular platform, you'll notice that, in addition to an API for which many language bindings exist, the download also includes the tool "xmllint". Using the --valid option, xmllint reports the following when passed the previous VoiceXML document:

hello.xml:6: validity error: No declaration for attribute audience of element hello
      
            ^
hello.xml:6: validity error: No declaration for element hello
      
            ^
hello.xml:5: validity error: Element hello is not declared in block list of poss
ible children
      
   ^

As an alternative to including the DOCTYPE declaration in the VoiceXML document, xmllint also supports a --dtdvalid switch.

Specify the URL to the DTD immediately following this switch:

xmllint --valid --dtdvalid http://www.w3.org/TR/voicexml20/vxml.dtd hello.vxml

If you're in the Java camp, you can make a few small modifications to the Java code listed above to enable DTD validation.

Modifications include:

1) Calling setValidating(true) on an instance of SAXParserFactory. Validation is off by default since it takes time to fetch a DTD and to process a document against it.

2) Adding error and warning methods to your subclass of DefaultHandler. DTD validation errors are reported through these callbacks, and the default implementation does nothing.

import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;

public class validate extends DefaultHandler
{
   public static final String USAGE = "Usage: java validate ";

   public void validate() {}

   public static void main(String [] args)
   {
      if (args.length < 1)
      {
         System.err.println(USAGE);
         System.exit(0);
      }

      try {
         SAXParserFactory factory = SAXParserFactory.newInstance();
         factory.setValidating(true); // enable DTD validation
         SAXParser saxParser = factory.newSAXParser();
         saxParser.parse(args[0], new validate());
      }
      catch(Exception ex)
      {
         System.err.println("Error: " + ex);
         System.exit(0);
      }

      System.out.println("'" + args[0] + "' is valid.");
   }

   // DTD errors
   public void error(SAXParseException e)
   throws SAXParseException
   {
      throw e;
   }

   // dump warnings too
   public void warning(SAXParseException err)
   throws SAXParseException
   {
      System.out.println("Warning "
         + "on line " + err.getLineNumber()
         + ", uri " + err.getSystemId());
      System.err.println(" " + err.getMessage());
   }
}

If you run this code against the VoiceXML document above containing the DOCTYPE declaration, you'll get the following:

      Error: org.xml.sax.SAXParseException: Element type "hello" must be declared.

The previous code example using MSXML and WSH doesn't require any changes to enable DTD validation. MSXML enables DTD validation by default. To disable DTD validation, set the validateOnParse property to false.

objParser.validateOnParse = false;



Continued...


back to the top

Copyright © 2001-2002 VoiceXML Forum. All rights reserved.
The VoiceXML Forum is a program of the
IEEE Industry Standards and Technology Organization (IEEE-ISTO).