Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: xml validation using an xsd file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Edmonton Canada
    Posts
    933

    Default xml validation using an xsd file

    We have a DF19.1 Webservice that reads an XML file into a Dataflex table.
    I would like to validate the xml file using an xsd file prior to processing it.

    I have found in the help some example code in the sections for AddExternalSchemaFile/Document and pbMultipleErrorMessage.
    Nice!!!

    Is there more? I was wondering if anyone knows of example applications that include code for validating xml using an xsd file that I could review and learn from.

    Thanks!

    Archie

  2. #2
    Join Date
    Mar 2009
    Location
    Edmonton Canada
    Posts
    933

    Default Re: xml validation using an xsd file

    Six months and ten days later I am back on this project again.
    Any suggestions or pointers would be appreciated.
    Thanks
    Archie

  3. #3
    Join Date
    Feb 2009
    Posts
    5,470

    Default Re: xml validation using an xsd file

    I did look into this area the best part of ten years ago and in the end did not end up using the xsd for validation

    In our case we were mostly creating the XML document to be sent to a government service

    If you are reading something from a service you would hope it would conform to their own schema!

    I think the reasons (which are a bit hazy now) for not using it were two fold

    The first being at the time I did not appreciate how much you could use XPath with df commands and this might have caused me to go further had I known

    However ultimately any errors reported need to be comprehensible to the user and code would need to parse the XPath to understand the context of the last element through its parentage. For example if it is something generic like 'amount' or the XML element names are 'teccy' and not obvious to the average user

    As we were mostly building the XML and reading some I wrote a set of classes that linked together via Procedure Set and Function that 'communicated' with structs matching the XML that would set/get the child information and any verification/error reporting was all done in DF code

    Also if there is an opportunity to correct bad data automatically e.g. truncating it then again DF code would be used.

    Ultimately verifying with an XSD is really just a raw problem stater it doesn't give you anything else.
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

  4. #4
    Join Date
    Mar 2009
    Location
    Edmonton Canada
    Posts
    933

    Default Re: xml validation using an xsd file

    Hello Focus
    Two things:
    (1) I appreciate your thoughts. I am doing validation of the XML and the Struct produced from the XML for the first time. Knowing that you have come to your conclusion is very helpful
    (2) Winnie quote. I have an 8-1/2 x 11 poster of his famous quote posted on the bulletin board of the small business office group where I have an office. I enjoy reading it every day. Others do too.

  5. #5
    Join Date
    Feb 2009
    Location
    Stuart, FL
    Posts
    5,322

    Default Re: xml validation using an xsd file

    we use schema validation when creating files before sending them to ensure compliant files. With some services this can be important.

    here is a small piece of code

    Code:
                Integer iErrorCount iErrorIdx hoErrorItem
                String sReason sXPath
                Set pbMultipleErrorMessages of hoXML to True
                Get AddExternalSchemaFile of hoXML  "http://www.starzen.com/myschema" sSchemaFile to bOk
                Get ValidateDocument of hoXML to hoParseErrorObject
                If hoParseErrorObject Begin        
                    Get piErrorCount of hoParseErrorObject to iErrorCount        
                    For iErrorIdx from 0 to (iErrorCount-1)
                        Get ErrorItemNode of hoParseErrorObject iErrorIdx to hoErrorItem
                        Get psReason of hoErrorItem to sReason
                        Increment iTotalErrorCount
                        
                        Get psErrorXPath of hoErrorItem to sXPath
    
                        // process error here
                        
                        Send Destroy of hoErrorItem
                        
                        //
                    Loop
                
                    Send Destroy of hoParseErrorObject
                End
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    IT Director at Balloons Everywhere

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

  6. #6
    Join Date
    Mar 2009
    Location
    Edmonton Canada
    Posts
    933

    Default Re: xml validation using an xsd file

    Hello Michael
    Much appreciate the code snippet!!!
    I will give that a go now.

  7. #7
    Join Date
    Mar 2009
    Location
    Edmonton Canada
    Posts
    933

    Default Re: xml validation using an xsd file

    Quote Originally Posted by ArchieCampbell View Post
    Hello Michael
    Much appreciate the code snippet!!!
    I will give that a go now.
    Good news is I got it to work.
    But one thing is still quite confusing.
    I thought "namespace" could be anything as long as it was the same in the .xsd, the xml and the DF code AddExternalSchemaFile.
    Apparently not.

    With the following code, XML and .XSD it did NOT work.
    The DF code below set bOk to false.

    To solve it I had to change http://www.starzen.com/myschema to http://www.w3.org/2001/XMLSchema in the DF code, the XML and the .XSD

    Code:
    DF code
    Get AddExternalSchemaFile of hoXML  "http://www.starzen.com/myschema" sSchemaFile to bOk
    
    XML
    <?xml version="1.0"?>
    <Invoice><xmlns="http://www.starzen.com/myschema"> 
    	<InvoiceNum>12344</InvoiceNum>
    </Invoice>
    
    .xsd file
    <?xml version="1.0" encoding="UTF-8"?>
     <xs:schema xmlns:xs="http://www.starzen.com/myschema" elementFormDefault="qualified" attributeFormDefault="unqualified">
             <xs:element name="Invoice">
                   <xs:complexType>
                         <xs:sequence>
                               <xs:element name="InvoiceNum" type="xs:string"></xs:element>
                          </xs:sequence>
                   </xs:complexType>
             </xs:element>
       </xs:schema>

  8. #8
    Join Date
    Feb 2009
    Location
    Stuart, FL
    Posts
    5,322

    Default Re: xml validation using an xsd file

    sorry that was my bad. I replaced the original content with the starzen thing without looking thinking it was one of our links
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    IT Director at Balloons Everywhere

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

  9. #9
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default Re: xml validation using an xsd file

    Archie

    In my understanding, the namespace prefix (i.e. "xs") can be anything you want, but it must refer back to the correct namespace definition (i.e. "http://www.w3.org/2001/XMLSchema").

    Mike

  10. #10
    Join Date
    Feb 2009
    Posts
    5,470

    Default Re: xml validation using an xsd file

    The definition can be anything you want too, it's just that the waters get muddy when you need to find the definition itself you need to know where to look. It's a bit like when you used to sign up to things you could have any username you wanted now it almost always has to be an email address. They are both just strings it's just and email address is easier for other purposes and you defiantly have the email address to contact etc
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •