2012-07-29 87 views
7

estoy tratando de validar un archivo .XML con un fichero de .XSD con MSXML 6.0 DOM sino en ejecutar el código que estoy recibiendo este mensaje de error:MSXML VBA: validación de XML contra XSD: "El '' espacio de nombres proporcionado difiere del targetNamespace del esquema ''.

Test.xsd#/schema/targetNamespace[1] 
"The '' namespace provided differs from the schema's 'http://somewhere.com/root' targetNamespace." 

A versiones muy simplificadas de los archivos .xml y archivos .xsd también producen el mismo error:

ARCHIVO XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns2:noderoot xmlns:ns2="http://somewhere.com/root"> 
    <general> 
     <year>2011</year> 
     <month>02</month> 
    </general> 
</ns2:noderoot> 

XSD ARCHIVO

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns="http://somewhere.com/root" 
      targetNamespace="http://somewhere.com/root" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      elementFormDefault="unqualified" 
      attributeFormDefault="unqualified"> 

    <xs:complexType name="TYPE_nodeGeneral"> 
     <xs:sequence> 
      <xs:element name="year"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
         <xs:length value="4"/> 
         <xs:pattern value="\d{4}"/> 
        </xs:restriction> 
       </xs:simpleType> 
      </xs:element> 
      <xs:element name="month"> 
       <xs:simpleType> 
        <xs:restriction base="xs:string"> 
         <xs:length value="2"/> 
        </xs:restriction> 
       </xs:simpleType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType>   

    <xs:complexType name="TYPE_noderoot"> 
     <xs:sequence> 
      <xs:element name="general" type="TYPE_nodeGeneral"></xs:element>    
     </xs:sequence> 
    </xs:complexType> 

    <xs:element name="noderoot" type="TYPE_noderoot"></xs:element> 

</xs:schema> 

Con el fin de validar el archivo XML que estoy usando el código escrito en VBA (Excel 2010):

Sub XSD_Validation() 

    XML_FILE = "I:\Test.xml"  
    XSD_FILE = "I:\Test.xsd" 

    Dim xmlDoc As MSXML2.DOMDocument60 
    Set xmlDoc = New MSXML2.DOMDocument60 
    xmlDoc.async = False 
    xmlDoc.validateOnParse = False 
    xmlDoc.resolveExternals = False 

    xmlDoc.Load XML_FILE 

    ' Open XSD file 
    Dim obXSD As MSXML2.DOMDocument60 
    Set objXSD = New MSXML2.DOMDocument60 
    objXSD.async = False 
    objXSD.Load XSD_FILE 

    ' Populate schema cache 
    Dim objSchemaCache As XMLSchemaCache60 
    Set objSchemaCache = New MSXML2.XMLSchemaCache60 
    objSchemaCache.Add "", objXSD 

    ' XSD XML Bind 
    Set xmlDoc.Schemas = objSchemaCache 

    'Error visualization 
    Dim objErr As MSXML2.IXMLDOMParseError 
    Set objErr = xmlDoc.Validate() 
    If objErr.errorCode <> 0 Then 
     Debug.Print "Error parser: " & objErr.errorCode & "; " & objErr.reason 
    Else 
     Debug.Print "No errors found" 
    End If 

    Set objErr = Nothing 
    Set objXSD = Nothing 
    Set objSchemaCache = Nothing 
    Set xmlDoc = Nothing 

End Sub 

El archivo XSD puede ser modificado pero el archivo XML debe permanecer intocable.

He estado tratando de resolver este problema durante más de 8 horas sin ningún resultado positivo.

Cualquier ayuda será muy apreciada.

Respuesta

11

Intente agregar el URI de espacio de nombres a la memoria caché de esquema.

Sub XSD_Validation() 
    Dim xmlDoc As MSXML2.DOMDocument60 
    Dim objSchemaCache As New XMLSchemaCache60 
    Dim objErr As MSXML2.IXMLDOMParseError 

    objSchemaCache.Add "http://somewhere.com/root", LoadXmlFile("I:\Test.xsd") 

    Set xmlDoc = LoadXmlFile("I:\Test.xml") 
    Set xmlDoc.Schemas = objSchemaCache 

    Set objErr = xmlDoc.Validate() 
    If objErr.errorCode = 0 Then 
     Debug.Print "No errors found" 
    Else 
     Debug.Print "Error parser: " & objErr.errorCode & "; " & objErr.reason 
    End If 
End Sub 

Function LoadXmlFile(Path As String) As MSXML2.DOMDocument60 
    Set LoadXmlFile = New MSXML2.DOMDocument60 

    With LoadXmlFile 
     .async = False 
     .validateOnParse = False 
     .resolveExternals = False 
     .load Path 
    End With 
End Function 
+0

Gracias Tomalak. Después de adaptar algunas partes de tu código al mío, ahora funciona como un amuleto. :) –

+0

@DavidGrant ¡Es bueno escuchar! – Tomalak

+0

+1 para una buena respuesta. –

1

No debería tener que cambiar nada en su xml/xsd, ya que juntos son válidos. El problema está en su código, por lo que le sugiero echar un vistazo a this sample que muestra lo que creo que es diferente de su código: cuando agrega el XSD a la caché, use el espacio de nombres de destino http://somewhere.com/root en lugar de la cadena vacía.

+0

Gracias Petru. Tuviste razón al señalar la naturaleza del problema. :) –

Cuestiones relacionadas