Validating Edited Instances


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Validating Edited Instances

  1. #1
    PJ Guest

    Validating Edited Instances

    I've sucessfully created an .xdr schema and have validated an instance
    against it. However, it appears that subsequent edits of the instance do
    not continue to validate against the schema. Is there any way to enforce
    schema validation upon edits? I would rather not reparse the whole document
    upon every edit.

    -----------------
    example schema:
    <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <ElementType name="wine" content="empty">
    <AttributeType name="id" dt:type="int"/>
    <AttributeType name="name" dt:type="string"/>
    <AttributeType name="type" dt:type="enumeration" dt:values="cabernet
    pinot port merlot"/>
    <attribute type="id"/>
    <attribute type="name"/>
    <attribute type="type"/>
    </ElementType>
    <ElementType name="cellar" content="eltOnly">
    <element type="wine" minOccurs="0" maxOccurs="*"/>
    </ElementType>
    </Schema>
    -----------------
    example instance:
    <cellar>
    <wine id="12" name="Kendall Jackson" type="port"/>
    </cellar>
    -----------------
    example code:
    Dim xmldoc As MSXML2.DOMDocument
    Dim xmlSchema As MSXML2.XMLSchemaCache
    Dim oE As MSXML2.IXMLDOMElement
    Dim strXPath As String

    Set xmldoc = New MSXML2.DOMDocument
    Set xmlSchema = New MSXML2.XMLSchemaCache
    ' add schema to collection
    Call xmlSchema.Add(NAMESPACE, SCHEMA)

    With xmldoc
    Set .schemas = xmlSchema
    .async = False
    If Not .Load(XML_INSTANCE) Then
    Call MsgBox("Error parsing:" & vbCrLf & .parseError.reason)
    End If
    strXPath = "//wine[@id='12']"
    Set oE = .documentElement.selectSingleNode(strXPath)
    End With

    ' this is an invalid value according to the allowed values
    ' of the schema defined attibute
    ' but there is no indication of this
    Call oE.setAttribute("type", "cranberry")
    -----------
    the instance validates fine against the schema when loading, hydrating
    ..parseError if an attribute value violates a datatype definition (including
    enumerations). but when the doc is edited there is no way of determining
    the edit violates the schema without loading the whole doc's string into a
    new doc instance, that I know of. Do you?

    any help is greatly appreciated~

    ps. please don't tell me i should be using .xsd instead of .xdr unless you
    can show me how i
    can easily duplicate the .dataType property and .definition property that
    allows me to access metadata such as dataTypes, minlength, maxLength,
    enumerated values, etc. thanks




  2. #2
    Russell Jones Guest

    Re: Validating Edited Instances

    From the MS SDK 4 docs
    The validate method performs run-time validation on the currently loaded
    document using the currently loaded document type definition (DTD), schema,
    or schema collection.

    objXMLDOMDocument2.validate();

    Remarks
    This method only validates fully loaded documents (readyState == 4).
    The validate method returns an IXMLDOMParseError that is independent of the
    value returned by the parseError property on a document. Only the errorCode
    and reason properties of the returned value are set.
    Unlike load, validate will fail if there is no DTD or schema applied to the
    document element. Therefore, validate will not be able tell you whether the
    document is just well-formed.
    The validate method does not parse new schemas, but can import a schema from
    a SchemaCache associated with the document through the schemas property. If
    there is no schema for a given namespace, the elements in that namespace
    will not be validated.

    HTH
    Russell Jones
    Sr. Web Development Editor,
    DevX.com

    "PJ" <pjwalNO@SPAMhotmail.com> wrote in message
    news:3b71bf19$1@news.devx.com...
    > I've sucessfully created an .xdr schema and have validated an instance
    > against it. However, it appears that subsequent edits of the instance do
    > not continue to validate against the schema. Is there any way to enforce
    > schema validation upon edits? I would rather not reparse the whole

    document
    > upon every edit.
    >
    > -----------------
    > example schema:
    > <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    > xmlns:dt="urn:schemas-microsoft-com:datatypes">
    > <ElementType name="wine" content="empty">
    > <AttributeType name="id" dt:type="int"/>
    > <AttributeType name="name" dt:type="string"/>
    > <AttributeType name="type" dt:type="enumeration"

    dt:values="cabernet
    > pinot port merlot"/>
    > <attribute type="id"/>
    > <attribute type="name"/>
    > <attribute type="type"/>
    > </ElementType>
    > <ElementType name="cellar" content="eltOnly">
    > <element type="wine" minOccurs="0" maxOccurs="*"/>
    > </ElementType>
    > </Schema>
    > -----------------
    > example instance:
    > <cellar>
    > <wine id="12" name="Kendall Jackson" type="port"/>
    > </cellar>
    > -----------------
    > example code:
    > Dim xmldoc As MSXML2.DOMDocument
    > Dim xmlSchema As MSXML2.XMLSchemaCache
    > Dim oE As MSXML2.IXMLDOMElement
    > Dim strXPath As String
    >
    > Set xmldoc = New MSXML2.DOMDocument
    > Set xmlSchema = New MSXML2.XMLSchemaCache
    > ' add schema to collection
    > Call xmlSchema.Add(NAMESPACE, SCHEMA)
    >
    > With xmldoc
    > Set .schemas = xmlSchema
    > .async = False
    > If Not .Load(XML_INSTANCE) Then
    > Call MsgBox("Error parsing:" & vbCrLf & .parseError.reason)
    > End If
    > strXPath = "//wine[@id='12']"
    > Set oE = .documentElement.selectSingleNode(strXPath)
    > End With
    >
    > ' this is an invalid value according to the allowed values
    > ' of the schema defined attibute
    > ' but there is no indication of this
    > Call oE.setAttribute("type", "cranberry")
    > -----------
    > the instance validates fine against the schema when loading, hydrating
    > .parseError if an attribute value violates a datatype definition

    (including
    > enumerations). but when the doc is edited there is no way of determining
    > the edit violates the schema without loading the whole doc's string into a
    > new doc instance, that I know of. Do you?
    >
    > any help is greatly appreciated~
    >
    > ps. please don't tell me i should be using .xsd instead of .xdr unless you
    > can show me how i
    > can easily duplicate the .dataType property and .definition property that
    > allows me to access metadata such as dataTypes, minlength, maxLength,
    > enumerated values, etc. thanks
    >
    >
    >




  3. #3
    PJ Guest

    Re: Validating Edited Instances

    yes that does, thank you very much.

    looks like the .validate method returns the .parseError object, so it's just
    a matter of checking:

    If xmlDoc.validate.errorCode <> 0 Then
    ' handle it
    End If

    very nice...thanks again!

    "Russell Jones" <arj1@northstate.net> wrote in message
    news:3b720ae1$1@news.devx.com...
    > From the MS SDK 4 docs
    > The validate method performs run-time validation on the currently loaded
    > document using the currently loaded document type definition (DTD),

    schema,
    > or schema collection.
    >
    > objXMLDOMDocument2.validate();
    >
    > Remarks
    > This method only validates fully loaded documents (readyState == 4).
    > The validate method returns an IXMLDOMParseError that is independent of

    the
    > value returned by the parseError property on a document. Only the

    errorCode
    > and reason properties of the returned value are set.
    > Unlike load, validate will fail if there is no DTD or schema applied to

    the
    > document element. Therefore, validate will not be able tell you whether

    the
    > document is just well-formed.
    > The validate method does not parse new schemas, but can import a schema

    from
    > a SchemaCache associated with the document through the schemas property.

    If
    > there is no schema for a given namespace, the elements in that namespace
    > will not be validated.
    >
    > HTH
    > Russell Jones
    > Sr. Web Development Editor,
    > DevX.com
    >
    > "PJ" <pjwalNO@SPAMhotmail.com> wrote in message
    > news:3b71bf19$1@news.devx.com...
    > > I've sucessfully created an .xdr schema and have validated an instance
    > > against it. However, it appears that subsequent edits of the instance

    do
    > > not continue to validate against the schema. Is there any way to

    enforce
    > > schema validation upon edits? I would rather not reparse the whole

    > document
    > > upon every edit.
    > >
    > > -----------------
    > > example schema:
    > > <Schema xmlns="urn:schemas-microsoft-com:xml-data"
    > > xmlns:dt="urn:schemas-microsoft-com:datatypes">
    > > <ElementType name="wine" content="empty">
    > > <AttributeType name="id" dt:type="int"/>
    > > <AttributeType name="name" dt:type="string"/>
    > > <AttributeType name="type" dt:type="enumeration"

    > dt:values="cabernet
    > > pinot port merlot"/>
    > > <attribute type="id"/>
    > > <attribute type="name"/>
    > > <attribute type="type"/>
    > > </ElementType>
    > > <ElementType name="cellar" content="eltOnly">
    > > <element type="wine" minOccurs="0" maxOccurs="*"/>
    > > </ElementType>
    > > </Schema>
    > > -----------------
    > > example instance:
    > > <cellar>
    > > <wine id="12" name="Kendall Jackson" type="port"/>
    > > </cellar>
    > > -----------------
    > > example code:
    > > Dim xmldoc As MSXML2.DOMDocument
    > > Dim xmlSchema As MSXML2.XMLSchemaCache
    > > Dim oE As MSXML2.IXMLDOMElement
    > > Dim strXPath As String
    > >
    > > Set xmldoc = New MSXML2.DOMDocument
    > > Set xmlSchema = New MSXML2.XMLSchemaCache
    > > ' add schema to collection
    > > Call xmlSchema.Add(NAMESPACE, SCHEMA)
    > >
    > > With xmldoc
    > > Set .schemas = xmlSchema
    > > .async = False
    > > If Not .Load(XML_INSTANCE) Then
    > > Call MsgBox("Error parsing:" & vbCrLf & .parseError.reason)
    > > End If
    > > strXPath = "//wine[@id='12']"
    > > Set oE = .documentElement.selectSingleNode(strXPath)
    > > End With
    > >
    > > ' this is an invalid value according to the allowed values
    > > ' of the schema defined attibute
    > > ' but there is no indication of this
    > > Call oE.setAttribute("type", "cranberry")
    > > -----------
    > > the instance validates fine against the schema when loading, hydrating
    > > .parseError if an attribute value violates a datatype definition

    > (including
    > > enumerations). but when the doc is edited there is no way of

    determining
    > > the edit violates the schema without loading the whole doc's string into

    a
    > > new doc instance, that I know of. Do you?
    > >
    > > any help is greatly appreciated~
    > >
    > > ps. please don't tell me i should be using .xsd instead of .xdr unless

    you
    > > can show me how i
    > > can easily duplicate the .dataType property and .definition property

    that
    > > allows me to access metadata such as dataTypes, minlength, maxLength,
    > > enumerated values, etc. thanks
    > >
    > >
    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center