Visual DataFlex 15.1 Sneak Peek - XML Changes
by, 19-Oct-2009 at 09:00 AM (3887 Views)
By the time this gets published, Visual DataFlex 15.1 is just about to enter Beta testing. One of the cool new things is that XML support has been updated. The changes include an internal update of the underlying MS XML standard Windows library to msxml6. And perhaps the most requested new feature is support for XML Schema validations.
These changes to the XML classes can be taken advantage of immediately as you update to VDF 15.1. From a code perspective you still use the same classes as before, only now you will have access to the new features as well. More about compatibility concerns later.
Performing XML Schema validations has been made very simple as we expect this to be very common. In most cases all you will have to do is specify the XML schema, and validations will be performed automatically as you load the XML document. For example:
If a validation error occurs, an error will be returned when loading/parsing the document(bLoadOk will be False), and you will be able to inspect the validation error in the same manner as any parsing error. There's a lot more you can do with XML Schemas, such as validating the document on demand, but that will have to be discussed in a separate article.Code:Get AddExternalSchemaFile of hoDoc "http://target-namespace" "C:\MySchemas\Customer.xsd" to bOk Get LoadXMLDocument of hoDoc to bLoadOk
Why should you even care about XML Schemas?
Often schema validations can come up as part of requirements for a project, and you may not think much more of it than that it's something you need to deal with but prefer not to. But what are schemas good for anyway? XML is the standard format for exchange of information between different businesses and computer systems, often via web services. Just as we are used to business rules for data entry by humans, we need business rules for automatic data exchanges, to ensure that the data received is accurate and consistent with the existing data in the system. The other system may have different and/or more relaxed rules about the data than you do, so you need to validate the data on your own.
As reading/writing XML documents is usually automated in software and not edited manually by a human with a text editor, it's very important that the structure of the document matches the structure expected by the code reading it. Subtle structural errors can result in severe consequences when interpreting the document. If you're lucky the program may crash or display an error, if you're not so lucky it may associate the wrong purchase order with the wrong customer, or deduct the wrong account or similar.
To ensure that the structure of the document is correct, you need to first validate the document and data before doing something interesting with it. Such manual validation checks can be very error prone, painstaking and time consuming to write. XML Schemas provides a way to automate the validation process to ensure the document indeed does match the structure and format that your code expects. If the document is valid according to the schema that your code has been written for, then you can be reasonably sure that you can parse the document and the data will be accurate and consistent without having to write hundreds of detailed validation checks of your own.
As more and more systems exchange and share information with each other, XML schemas become increasingly important to ensure data meets the particular industry standard quality requirements. The health care industry, for example, has a huge investment into developing such standards, where the rules are all expressed as XML schemas.
Other new XML features
Some other changes include access to DOM second level properties (psDomSecondLevelProperty etc.), to control the behavior of the parser and other things in more detail. Support for detecting multiple parsing/validation errors (pbMultipleErrorMessages). Previously the parser would stop at the first error in the document, you can now configure it to continue and try to detect multiple errors at the same time. You can also iterate programmatically over all the parsing/validation errors with piErrorCount and ErrorItemNode.
Compatibility Concerns in VDF 15.1
As mentioned above, the underlying XML support has been updated from msxml3 to msxml6, and no special code changes are required to take advantage of that. Simply updating to VDF 15.1 will automatically cause your programs to use msxml6 as the underlying XML support library.
There's however a compatibility/downgrade configuration option, which when used will automatically use msxml3 just as in VDF 15.0 and earlier. If you use this configuration option, none of the new features will be available, and everything will work exactly as before. This option is global to the specific program, and cannot be switched back and forth at will. Once the compatibility switch is turned on, the specific program (.exe) will use the old msxml3 library until it exits.
In most cases all your XML code will work exactly the same with VDF 15.1 and msxml6, and there's no need to use the legacy msxml3 configuration option. But there are a few compatibility issues if you happened to take advantage of some of the non-standard features in msxml3 that MS replaced with standards conforming features in msxml6. In such case you have two options, you can modify the code (usually one or two lines of code) so it works with the new standards conforming behavior of msxml6, or you can temporarily use the compatibility switch and continue to use msxml3 as before. The compatibility option is 100% compatible with VDF 15.0, as it effectively uses the exact same code as in VDF 15.0 and earlier.
As I said, in all likelihood your XML code will continue to work exactly as before with VDF 15.1 and msxml6 without any changes and without using the compatibility option. We were pleasantly surprised ourselves how compatible msxml6 is with msxml3, and the few incompatibilities are very understandable as they're essentially changes for the better to comply with the W3C XML standard.
The one compatibility issue that you should look for in your code are any uses of FindNode and FindNodeList in combination with use of namespaces. Most uses of FindNode and FindNodeList probably only deal with XML documents without namespaces, in which case it will continue to work as before and you don't need to make any changes. It's only uses of FindNode and FindNodeList with XML documents that use namespaces where you may need to make minor modifications to your code. The changes are minimal, and typically involves adding one line of code and changing one or a few string literals related to FindNode and FindNodeList.
Use of namespaces with FindNode and FindNodeList was a little ad-hoc in msxml3, and MS overhauled that in msxml6 to conform to W3C standards. Describing this in detail is beyond the scope of this article, and something I'll have to come back to separately. But in essence, before you can use namespaces with FindNode and FindNodeList, you must now declare the namespace prefixes by setting the new psSelectionNamespaces property. The changes are usually very simple and straightforward like this:
New code:Code:Move ("xsl:template/html/head") to sQueryString Get FindNode of hoRoot sQueryString to hoHead
As mentioned before, the alternative is of course to use the compatibility option, USE_LEGACY_XML_LIB, near the top of the .src file. In which case no code changes are required and everything will remain 100% backwards compatible with VDF 15.0 and continue to use msxml3.Code:Set psSelectionNamespaces of hoDoc to "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:html='http://www.w3.org/1999/xhtml'" Move ("xsl:template/html:html/html:head") to sQueryString Get FindNode of hoRoot sQueryString to hoHead
As for reference, we found only one use of FindNode with namespaces in the Studio source code for example, which we easily updated in a matter of minutes. It's expected that the number of instances that needs to be updated will be very few, if any, for most developers. Updating the code is very straightforward and in most cases takes little more than a few minutes.Code:Use DFAllent.pkg USE_LEGACY_XML_LIB
In summary, cool new changes to XML support in VDF 15.1, XML Schemas being the most notable change. Most code will just continue to work as before without any changes in 15.1. We obviously recommend that you search for any use of FindNode and FindNodeList in your code to see if it's used in combination with namespaces. If you find such uses, we recommend making the minor modifications as mentioned above. In some cases you may want to temporarily use the compatibility switch to ensure everything else is working before making the code changes.