Results 1 to 6 of 6

Thread: IXMLDOMNode.definition

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7

    Angry IXMLDOMNode.definition

    Sorry if this has been asked before or is just plain stupid, I'm new to all this.

    After applying a Schema to an XMLDOMDocument40 object, I'm trying to work out which Schema rule has caused a failure. I can tell which element in the XML is wrong and want to use this to et the Schema rule that deals with it. I was under the impression that IXMLDOMNode.definition could do this but I can't for the life of me get it to work.

    If anyone could post a short working example, I would be eternally grateful.

    I'm using MSXML 4 and VB6.

    Thanks in advance,
    -Dan Evans

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    There's some stuff in here that you have to change/remove but this is basically what you want to do.

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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    Thanks for the response. I should have made myself clearer - I have a piece of code similar to that which you have posted; however I would like to extend it so that as well as displaying the piece of XML that has broken the Schema, I would like to display the Schema rule that it has broken.

    I had assumed that the IXMLDOMNode.definition property would be ideal for this, acting as a pointer to the definition of the node in the Schema but I cannot seem to get it to work. In fact, even with XML that does match the Schema perfectly, the .definition property is always Nothing.

    Any thoughts?

    Thanks again,
    -Dan Evans

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    That page is similar to the one I found first whilst searching the MSDN. It makes it look so simple that I must be doing something wrong. Perhaps I am not hooking the Schema up to the XML object properly. I am using the technique that you listed above though and I've got the impression that this is the standard way of doing it.

    Taken straight from the MSDN:
    "Returns the XML-Data Schema ElementType for a given ELEMENT node. For example, given the element <myelement>, the definition method returns the node <ElementType name="myelement"> in the specified external schema. Returns Null when a DTD is used or when no schema is present."

    I have an element node called <moon> and a Schema rule <Element name="moon"> and it still doesn't work. Does this mean that I MUST use the <ElementType> elements, which means I MUST use XDR Schemas (which my colleague is eager to tell me are a pile of ...)?

    I'm beginning to suspect that this is simply one of those things that Cannot Be Done.

    Thanks,
    -Dan Evans

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    7
    Having just tried the same example using XDR instead of normal Schema's, the .definition property worked. I can only assume then that this property is only usable when using Microsofts XDR Schemas. Dead end

    Can anyone think of any other way to find out which Schema rule has been broken by a particular XML element?

    Thanks,
    -Dan Evans

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