CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    16

    Need help validating XML against XSD

    I've made a program that checks a folder for xml files, converts them to text files and then saves the text files in another folder. Before I check the folder for xml files, however, I'd like to find out if the XML document contains all the data that's required, and if it doesn't, the program wont convert it at all.

    I'm using the code found here: http://www.nonhostile.com/howto-vali...xsd-in-vb6.asp

    My XSD file looks like this:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:n="urn:sample" targetNamespace="urn:sample">
    <xs:element name="sampleRoot" type="n:sampleRootType"/>

    <xs:complexType name="sampleRootType">
    <xs:attribute name="so_nbr" type="n:so_nbr"/>
    <xs:attribute name="so_custpo" type="n:so_custpo"/>
    <xs:attribute name="so_spname" type="n:so_spname"/>
    <xs:attribute name="so_sotcno" type="n:so_sotcno"/>
    </xs:complexType>

    <xs:simpleType name="so_nbr">
    <xs:restriction base="xs:string">
    <xs:enumeration value="so_nbr"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="so_custpo">
    <xs:restriction base="xs:string">
    <xs:enumeration value="so_custpo"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="so_spname">
    <xs:restriction base="xs:string">
    <xs:enumeration value="so_spname"/>
    </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="so_sotcno">
    <xs:restriction base="xs:string">
    <xs:enumeration value="so_sotcno"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:schema>

    I've changed the code from the original version because I need the program to check the xml files for each of the values given , rather than just one (like in the example in the url). When I run the program, I get the following error: Run-time error '1': Validate failed because the root element had no associated DTD/schema. I've been trying to find the solution for hours, but apparently a lot of people have the same problem also. Any help would be appreciated.

  2. #2
    Join Date
    Jun 2009
    Posts
    16

    Re: Need help validating XML against XSD

    I made some changes to the xsd file, I still get the same error though.

    XSD:
    <?xml version="1.0" ?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com"
    xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">

    <xs:element name="record">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="so">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="so_nbr" type="xs:string"/>
    <xs:element name="so_custpo" type="xs:string"/>
    <xs:element name="so_spname" type="xs:string"/>
    <xs:element name="so_sotcno" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    and here's the XML file I'm testing with:

    <?xml version="1.0"?>
    <record>
    <so>
    <so_nbr>so_nbr</so_nbr>
    <so_custpo>so_custpo</so_custpo>
    <so_type>so_type</so_type>
    <so_spname>so_spname</so_spname>
    </so>
    </record>

    I can't find anything wrong with the files, but I did find a short explanation about the error: "Validate failed because the root element had no associated DTD/schema = usually indicates there is a mismatch between the schema and the XML instance document." I'm still having trouble finding the cause of the error though. Also, I'm a bit unsure what the targetNamespace is for. Any help would be appreciated.

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

Featured