Results 1 to 5 of 5

Thread: Why does this XML ge validated ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Why does this XML ge validated ?

    Hello everybody,


    I use msxml version4 and I want to validate a xml file against a XDR file.
    When I change for example this to type int <ElementType name="CompanyName" dt:type="string" /> then I would expect to receive an error... but I don't get any errors at all !

    Does anyone know what should be wrong ?

    I hope someone could help me out !

    Thanks,

    Bjorn

    here's my XDR file :
    VB Code:
    1. <?xml version="1.0" ?>
    2. <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    3.         xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"  
    4.         xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    5.  
    6.    <ElementType name="CustomerID" dt:type="int" />
    7.    <ElementType name="CompanyName" dt:type="int" />
    8.    <ElementType name="City" dt:type="string" />
    9.  
    10.    <ElementType name="ROOT" sql:is-constant="1">
    11.       <element type="Customers" />
    12.    </ElementType>
    13.  
    14.    <ElementType name="Customers"  sql:relation="Cust" >
    15.       <element type="CustomerID"  sql:field="CustomerID" />
    16.       <element type="CompanyName" sql:field="CompanyName" />
    17.       <element type="City"        sql:field="City" />
    18.  
    19.    </ElementType>
    20. </Schema>

    This is my XML file :
    VB Code:
    1. <ROOT>
    2.   <Customers>
    3.     <CustomerID>1111</CustomerID>
    4.     <CompanyName>Sean Chai</CompanyName>
    5.     <City>NY</City>
    6.   </Customers>
    7.   <Customers>
    8.     <CustomerID>1112</CustomerID>
    9.     <CompanyName>Tom Johnston</CompanyName>
    10.      <City>LA</City>
    11.   </Customers>
    12.   <Customers>
    13.     <CustomerID>1113</CustomerID>
    14.     <CompanyName>Institute of Art</CompanyName>
    15.   </Customers>
    16. </ROOT>

    And this is my code to validate :
    VB Code:
    1. Dim xmlschema As MSXML2.XMLSchemaCache40
    2.         xmlschema = New MSXML2.XMLSchemaCache40()
    3.         xmlschema.add("", "c:\temp\temp2\SampleSchema.xml")
    4.  
    5.         'Create an XML DOMDocument object.
    6.         Dim xmldom As MSXML2.DOMDocument40
    7.         xmldom = New MSXML2.DOMDocument40()
    8.  
    9.         'Assign the schema cache to the DOM document.
    10.         'schemas collection.
    11.         xmldom.schemas = xmlschema
    12.  
    13.  
    14.         'Load books.xml as the DOM document.
    15.         xmldom.async = False
    16.         xmldom.validate()
    17.  
    18.         xmldom.load("c:\temp\temp2\Samplexml.xml")
    19.  
    20.         'Return validation results in message to the user.
    21.         If xmldom.parseError.errorCode <> 0 Then
    22.             MsgBox(xmldom.parseError.errorCode & " " & _
    23.             xmldom.parseError.reason)
    24.         Else
    25.             MsgBox("No Error")
    26.         End If

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here is my code which does work. It contains some extra stuff, but you can modify it.

    Edit: MSXML4 is required.

    VB Code:
    1. Public Sub ValidateXML()
    2. '***************************************************************************
    3. 'Purpose: Validate the modified xml file against the schema
    4. 'Inputs:  None
    5. 'Outputs: Error display
    6. '***************************************************************************
    7.  
    8.     Dim oxmlSchema As MSXML2.XMLSchemaCache40
    9.     Dim oxmlValidateDoc As MSXML2.DOMDocument40
    10.     Dim strText As String
    11.    
    12.     Screen.MousePointer = vbHourglass
    13.    
    14.     On Error GoTo ErrorRoutine
    15.    
    16.     'Create the schema cache and add the schema to it.
    17.     Set oxmlSchema = New MSXML2.XMLSchemaCache40
    18.     oxmlSchema.Add "", frmFileLoc.SchemaLocation & "\" & gstrSchemaName
    19.    
    20.     'Create an XML DOMDocument object.
    21.     Set oxmlValidateDoc = New MSXML2.DOMDocument40
    22.    
    23.     'Assign the schema cache to the schemas collection.
    24.     Set oxmlValidateDoc.schemas = oxmlSchema
    25.    
    26.     PrettyPrint True
    27.    
    28.     'Load the xml file as the DOM document.
    29.     oxmlValidateDoc.async = False
    30.     oxmlValidateDoc.Load gstrTempFileName
    31.    
    32.     Screen.MousePointer = vbNormal
    33.    
    34.     'Return validation results in message to the user.
    35.     If oxmlValidateDoc.parseError.errorCode <> 0 Then
    36.         If oxmlValidateDoc.parseError.errorCode = -2146697210 Then
    37.             Err.Raise 1004
    38.         Else
    39.             ' Remove tabs from the text
    40.             strText = Replace(oxmlValidateDoc.parseError.srcText, Chr$(9), "")
    41.             frmValidate.lblResults.Alignment = vbLeftJustify
    42.             frmValidate.lblResults.Caption = _
    43.                     "Error: " & oxmlValidateDoc.parseError.reason _
    44.                     & vbCrLf _
    45.                     & "Found in line number " & oxmlValidateDoc.parseError.Line _
    46.                     & "  line position " & oxmlValidateDoc.parseError.linepos & "." _
    47.                     & vbCrLf & vbCrLf _
    48.                     & strText _
    49.                     & vbCrLf & vbCrLf _
    50.                     & "(Other errors may be present.)"
    51.             frmValidate.imgSad.Visible = True
    52.             frmValidate.imgHappy.Visible = False
    53.             frmValidate.Show vbModal
    54.             frmMain.sbarStatus.Panels(1).Text = "Knowledge base validation failed"
    55.         End If
    56.     Else
    57.         frmValidate.lblResults.Alignment = vbCenter
    58.         frmValidate.lblResults.Caption = vbCrLf & vbCrLf _
    59.                                        & "Knowledge base validation passed"
    60.         frmValidate.imgSad.Visible = False
    61.         frmValidate.imgHappy.Visible = True
    62.         frmValidate.Show vbModal
    63.         frmMain.sbarStatus.Panels(1).Text = "Knowledge base validation passed"
    64.     End If
    65.    
    66.     Set oxmlSchema = Nothing
    67.     Set oxmlValidateDoc = Nothing
    68.  
    69.     Exit Sub
    70.    
    71. ErrorRoutine:
    72.  
    73.     If Err.Number = 1004 Then
    74.         MsgBox ResolveResString(resMissingTempFile), _
    75.                vbExclamation + vbOKOnly, _
    76.                ResolveResString(resErrorTitle, "|1", "Validation")
    77.     Else
    78.         DisplayError "ValidateXML"
    79.     End If
    80.    
    81. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    Hi Martin,

    thanks for your answer but as I can see, there isn't that much difference between our code. There's only more errorhandling.
    The thing is that I don't get an error.

    Could you please verify my 2 files against my (or your) code ?

    Thanks a lot !

    Bjorn

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Since my current program expects a specific XML file, I would have to write a new program to do that. The main differences that I see between your code and mine is your xmldom.validate() line and that I use Set and you don't. What is .validate()? Do you need it? The xml document will be validated when the .load happens.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    Hello Martin,


    I found the error

    <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"
    xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    this needs to be:
    <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    xmlns:dt="urn:schemas-microsoft-com:datatypes"
    xmlns:sql="urn:schemas-microsoft-com:xml-sql">

    I hope this will give even a wider experience then you've already got !

    Thanks for the help,

    Bjorn

Posting Permissions

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



Click Here to Expand Forum to Full Width