View RSS Feed

Development Team Blog

Adjusting your XML Application to use Namespace and XML Schema Validation

Rate this Entry
In order to update our Samples to use the new XML methods (*NS methods) and schema validation, I had to go through some steps to get it all working. I thought it would be beneficial to document what I have done and share what I have learned while converting the samples -- so here I am.

Note that these changes are not required for applications to be migrated to Visual DataFlex 15.1, but namespaces can play a vital role in a data integration project or data exchange scenario, where items names can often come into conflict. Namespaces help identifying each element and avoid confusion, and together with schema validation, may increase the reliability of applications.

If you would like to use schema validation and the namespace methods, you will need to update your application and these are the steps I would recommend for updating an XML application:
1. Understand the changes and concepts
2. Modify XML documents
3. Generate Schema
4. Adjust Stylesheets (XSL)
5. Update source code
6. Test
1. Understand the changes and concepts
In order to understand how the changes affect your applications, you need to learn about the changes that were made and understand them.

You should start by reading the Help - "XML Improvements" and "XML Namespaces Primer" are good starting points. Then, read the various blogs on XML posted by Sonny Falk and John Tuohy, such as Practical XML namespaces and Visual DataFlex 15.1 - XML Optimizations, to understand namespacing and the changes required when using Visual DataFlex 15.1 and MSXML6.

As for the concepts, you may find tons of resources by simply searching the internet, but you should try:
http://www.w3schools.com/xml/default.asp
http://www.xml.com/index.csp
http://www.w3.org/standards/xml/

After you understand the changes, explore the samples (Specialized Components workspace, XML-Sample project) to see how it all works. The converted sample will be installed as part of Visual DataFlex 16.0 Studio.


2. Modify XML documents
To work with namespaces, your XML documents will need to include a namespace.

For example, in the sample, the input XML document Customer.xml had to be modified to replace
Code:
<CUSTOMERLIST>
with
Code:
<CUSTOMERLIST xmlns="http://www.dataaccess.com/CustomerList">
which includes the default namespace used in the document.


3. Generate Schema
XML Schemas can be used in document validation (XML Schemas specify the allowed struture and content of an XML document) and in model definition (XML Schemas describe tags and data types, and provide better understanding of the document structure). Applications may use XML Schemas to automatically validate XML documents.

Our samples used a DTD file to validate the XML document being read. When looking into converting our DTD to an XML Schema, the first thing I did was to find an XML editor. I chose XMLSpy.

XMLSpy is a good tool and has a great tutorial. If you do not already have a tool of your preference, I recommend giving it a try. Converting the DTD to XML Schema was very easy, just one menu option away. After editing the generated schema to specify the namespace I needed, the schema was ready to be used.

As with any automatically generated file, the output created by XMLSpy could also be improved, but the bulk of the work had been done.

If you already know enough about schemas, you may further edit the automatically generated file to fine-tune the rules it defines. Otherwise, use the generated file, and try to understand the structure defined and its impact on document validation. A good source of reference to understand schemas is Sonny's blog Introducton to XML Schemas. If you have not read it yet, it is a good place to start.

In my case, after I first converted the sample's DTD into XML Schema, John Tuohy pointed out that the structure was a bit complex and it would be a good idea to simplify it. So the schema you will find in the sample was created in a two-step process: converted from the original DTD and then tweaked into a simpler structure.


4. Adjust Stylesheets (XSL)
If you defined a stylesheet to be used with your application, you will need to adjust it to use namespace as well.
What was simply
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="CUSTOMERLIST">
will need to be changed to something like this:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:custList="http://www.dataaccess.com/CustomerList">
<xsl:template match="custList:CUSTOMERLIST">
5. Update source code
With all the files used by the application already updated, now the application itself needs to be modified.
  • Replace the old methods with their equivalent *NS methods
    With the namespace defined in your XML document, the first thing to do in your application is to replace the old methods with their equivalent *NS methods, such as AddElement -> AddElementNS, AttributeValue -> AttributeValueNS, and ChildNodeValue -> ChildElementValueNS.

A list of the new methods can be found in the Help -- see cXMLDOMDocument class, and click on the link "Processing XML Documents with Namespaces."
  • Create a prefix to refer to the namespace used
    Some methods, like FindNode and FindNodeList, must have their queries adjusted to use namespace as well. You do that by adding a prefix that identifies the namespace being used to the elements referenced in the query.

First, the prefix needs to be added to the list of namespaces stored in psSelectionNamespaces. As you can see in the sample, this may be done with the code below:
Code:
:
// Namespace to be used
Move "http://www.dataaccess.com/CustomerList" to sNamespace 
 
Set psSelectionNamespaces of hoXML to ("xmlns:custList='" - sNamespace    - "'")
 
Get LoadXMLDocument Of hoXML To bOK
Get DocumentElement Of hoXML To hoRoot
 
Get FindNodeList of hoRoot "custList:CUSTOMER" to hoList
:
  • Add XML Schema validation
    In order to use schema validation you will need to add to the schema cache the XML schema to be used for validation. You may do that by calling AddExternalSchemaFile:

Code:
Move "http://www.dataaccess.com/schemas/Customer" to sSchema
Get AddExternalSchemaFile of hoXML sSchema (sPath - "\Customer.xsd") to bOK
Get LoadXMLDocument Of hoXML To bOK
To have an XML document validated at loading time, call AddExternalSchemaFile before calling LoadXMLDocument like the example above. To validate a document after it has been loaded, use
Code:
Move "http://www.dataaccess.com/schemas/Customer" to sSchema
Get AddExternalSchemaFile of hoXML sSchema (sPath - "\Customer.xsd") to bOK
Get ValidateDocument of hoXML to hoParseErrorObject
6. Test
This is the final step to validate all the work you have done so far. Make sure to use the debugger (breakpoints, locals, call stack and watch windows) to examine the values set and read.

Once the application passes all your tests and the XML documents it generates conform with the specifications used, you are done!

Comments