Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2008, 11:11 AM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with validating vanila XML with XSD

Need help in validating with the existing XML and existing XSD:
---------------------------------------------------------------

These are the entities used:
----------------------------
XSD FILE (mainInfo.xsd):
========================

    code:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:abc="http://somesite.com/schemas/BaseDataTypes" targetNamespace="http://somesite.com/schemas/BaseDataTypes">
        <xsd:include schemaLocation="http://somesite.com/schemas/BaseDataTypes.xsd"/>
    
        <xsd:element name="mainDetail">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="City" type="abc:String30DataType"/>
                    <xsd:element name="State" type="abc:String30DataType"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    

        <xsd:element name="mainHeader">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element ref="abc:TotalRecords"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    

        <xsd:element name="mainTag">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element ref="abc:mainHeader"/>
                    <xsd:element ref="abc:mainDetail"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    
    </xsd:schema>



XML File:
=========

    code:


    <?xml version="1.0" encoding="UTF-8"?>
    <mainTag>
     <mainHeader>
             <TotalRecords><![CDATA[1]]></TotalRecords>
     </mainHeader>
     <mainDetail>
         <City><![CDATA[Chicago]]></City>
         <State><![CDATA[Illinois]]></State>
     </mainDetail>
    <mainTag>



ERROR MESSAGE:
==============
Error : cvc-elt.1: Cannot find the declaration of element 'mainTag'
IF I CHECK WITH FOLLOWING STARTING TAG IT WORKS PERFECTLY But following way we can't change from the thirdparty's XML:
=======================================
<mainTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://somesite.com/schemas/BaseDataTypes"
xsi:schemaLocation="http://somesite.com/schemas/BaseDataTypes http://somesite.com/schemas/mainInfo.xsd">

I USED THE FOLLOWING CODE FOR VALIDATING AGAINST THE XSD:
================================================== =======

    quote: import java.io.File;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;


    public class DomValidator {
    static final String schemaSource = "c:\\testProject\\mainInfo.xsd";

    static final String xml_file = "c:\\testProject\\profile.xml";

    static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";

    static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

    public static void main(String args[]) {


    try {
    DocumentBuilderFactory factory = DocumentBuilderFactory
    .newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);


    factory.setAttribute(JAXP_SCHEMA_LANGUAGE,
    "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));


    DocumentBuilder domParser = factory.newDocumentBuilder();
    // Set Error Handler
    ErrorChecker errors = new ErrorChecker();
    domParser.setErrorHandler(errors);

    Document document = domParser.parse(new File(xml_file));
    List errorList = errors.getErrors();
    Iterator iter = errorList.iterator();
    while (iter.hasNext()) {
    String errroMessage = (String) iter.next();
    System.out.println(errroMessage);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    }

    // to capture the errors and warnings and the fatal errors occured

    class ErrorChecker extends DefaultHandler

    {
    public ArrayList errorList = new ArrayList();

    public ErrorChecker() {
    }

    public List getErrors() {
    return errorList;
    }

    public void error(SAXParseException e) {
    errorList.add("Error : " + e.getMessage() + " " + e.getPublicId() + " "
    + e.getSystemId());
    }

    public void warning(SAXParseException e) {
    errorList.add("Warning : " + e.getMessage());
    }

    public void fatalError(SAXParseException e) {
    errorList.add("Fatal error : " + e.getMessage());
    System.exit(1);
    }

    }



It would be great if anyone can suggest me without changing the existing XML structure to validate properly by changing in My Java program. Or guide me to the right direction would be appreiciated.
 
Old August 21st, 2008, 11:15 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well your schema says
  targetNamespace="http://somesite.com/BaseDataTypes"
if you don't want that then remove that attribute from the schema.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 21st, 2008, 11:24 AM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It complains if I remove this:
targetNamespace="http://somesite.com/schemas/BaseDataTypes"
    that the following should be same as the the target name:
schemaLocation="http://somesite.com/schemas/BaseDataTypes.xsd"
 
Old August 21st, 2008, 11:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well if BaseDataTypes.xsd also has the targetNamespace attribute set then you need to remove that as well if you want to include the schema. You can only include a schema targetting the same namespace. Otherwise you would need an import and not an include.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 21st, 2008, 11:41 AM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is it possible to send me with the code snippet(for XSD AND BaseDataTypes) for this simple example to try with as I tried many ways but have no luck yet. The BaseDataTypes is refered here for :
<xsd:schema targetNamespace="http://somesite.com/schemas/BaseDataTypes">
<xsd:simpleType name="String30DataType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

So please update my XSDs with your suggestion would be great , so that I can try here. Thanks

 
Old August 21st, 2008, 11:48 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="String30DataType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  >
        <xsd:include schemaLocation="http://somesite.com/schemas/BaseDataTypes.xsd"/>

        <xsd:element name="mainDetail">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="City" type="String30DataType"/>
                    <xsd:element name="State" type="String30DataType"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>


        <xsd:element name="mainHeader">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element ref="TotalRecords"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>


        <xsd:element name="mainTag">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element ref="mainHeader"/>
                    <xsd:element ref="mainDetail"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>

    </xsd:schema>
Untested.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 21st, 2008, 01:42 PM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got the following error still :(

Error : src-include.2.1: The targetNamespace of the referenced schema, currently 'http://somesite.com/schemas/BaseDataTypes', must be identical to that of the including schema, currently 'null'. null http://somesite.com/schemas/BaseDataTypes.xsd








Similar Threads
Thread Thread Starter Forum Replies Last Post
XML to XSD gagsy Pro Java 0 March 3rd, 2008 02:38 AM
XML To XML, using XSL & XSD supercop75 XSLT 1 April 8th, 2006 02:48 AM
Validating XML against XSD ShaileshShinde General .NET 0 September 20th, 2005 01:22 AM
Validating Xml files with XSD file ShaileshShinde VB.NET 2002/2003 Basics 1 September 12th, 2005 07:00 AM
ASP with xml (along with .xsd) software_developer_kk Classic ASP Basics 0 April 18th, 2005 12:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.