You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by em <wh...@gmail.com> on 2015/08/14 21:25:32 UTC

Different target namespace error

Hi,

I've been struggling to resolve this issue for a week now and haven't gotten
any responses on StackOverflow
(http://stackoverflow.com/questions/31971324/error-different-target-namespace-but-schema-validates-and-namespaces-match),
so I found this list.

I managed to reduce my original complex files down to shorter examples which
demonstrate the issue. I'm able to validate this XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<My_Data>
    <PartOne>
        testing
    </PartOne>
    <PartTwo>
        123
    </PartTwo>
</My_Data>

with this XSD:

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="My_Data">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="PartOne" type="xs:string" minOccurs="0"
maxOccurs="1"/>
                <xs:element name="PartTwo" type="xs:integer" minOccurs="0"
maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

However, as soon as I add a targetNamespace attribute in the schema, I get
the error "has a different target namespace from the one specified in the
instance document ."

XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<My_Data
    xmlns="http://www.mywebsite.com/schema/myschema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <PartOne>
        <ElemOne>thing</ElemOne>
    </PartOne>
    <PartTwo>123</PartTwo>
</My_Data>

XSD:

<xs:schema
        xmlns="http://www.mywebsite.com/schema/myschema"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified"
        targetNamespace="http://www.mywebsite.com/schema/myschema">
    <xs:element name="My_Data">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="PartOne" type="SubType" minOccurs="0"
maxOccurs="1"/>
                <xs:element name="PartTwo" type="xs:integer" minOccurs="0"
maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="SubType">
        <xs:sequence>
            <xs:element name="ElemOne" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

But the XML and XSD validate! Why would this be? I've tried adding a
schemaLocation attribute and searched for other examples, but can't figure
out what is going on here.

Any insight would be greatly appreciated!

Thanks,

em


Re: Different target namespace error

Posted by Alberto Massari <al...@tiscali.it>.
Il 18/08/15 21:53, em ha scritto:
> Jeroen N. Witmond <jnw <at> xs4all.nl> writes:
>
>> On 2015-08-14 21:25, em wrote:
>>> Hi,
>>>
>>> I've been struggling to resolve this issue for a week now and haven't
>>> gotten
>>> any responses on StackOverflow
>>>
> (http://stackoverflow.com/questions/31971324/error-different-target-namespace-but-schema-validates-and-namespaces-match),
>>> so I found this list.
>> I've tried to recreate your problem but failed for lack of information;
>> see below.
>>
>>> (snip)
>> Please provide the exact commands that demonstrate the above file and
>> schema are valid.
>>
>>
>>> (snip)
>> Please provide the exact commands that demonstrate the above file and
>> schema are NOT valid; that is, the commands that result in the error
>> message you quoted above,.
>>
>>
> Thanks for the reply, and apologies for lack of information. I just used an
> online tool to verify my schema and XML:
> http://www.utilities-online.info/xsdvalidation/ since I am new to XSD /
> Xerces. The process for validating is inside of a rather complicated program
> so I had to pare it down quite a bit.
> Here is my C++ code:
>
> // schemasend.cc, compile with:
> // g++ -g -Wall -pedantic -L /usr/lib -o schemasend schemasend.cc
> CustomParserErrorHandler.cc -lxerces-c
>
> #include <xercesc/framework/MemBufInputSource.hpp>
> #include "CustomParserErrorHandler.hh"
> #include <string>
> #include <iostream>
>
> int main(int argc, char* argv[])
> {
>    xercesc::XMLPlatformUtils::Initialize();
>    std::cout << "Intialized" << std::endl;
>    try {
>      // Parse document
>
>      // This works:
>      //const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
> standalone=\"no\"
> ?><My_Data><PartOne>testing</PartOne><PartTwo>123</PartTwo></My_Data>";
>      //static std::string aXSDFilePath("MySchemaSimple.xsd");
>     
>      const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
> standalone=\"no\" ?><My_Data
> xmlns=\"http://www.mywebsite.com/schema/myschema\"
> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema\"><PartOne><ElemOne>thing</ElemOne></PartOne><PartTwo>123</PartTwo></My_Data>";
>      static std::string aXSDFilePath("MySchema.xsd");
>      
>      CustomParserErrorHandler aErrorHandler;
>
>      const xercesc::MemBufInputSource aInputSource(
>        (const XMLByte*)aXML.c_str(),
>        static_cast<uint>(aXML.size()),
>        "stringBuffer");
>
>      xercesc::XercesDOMParser* aParser = new xercesc::XercesDOMParser();
>      aParser->cacheGrammarFromParse(true);
>
>      aParser->setErrorHandler(&aErrorHandler);
>      aParser->setDoNamespaces(true);
>      aParser->setDoSchema(true);
>      aParser->setValidationSchemaFullChecking(true);
>      aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
>      aParser->setExternalNoNamespaceSchemaLocation((char*)NULL);
>      aParser->setExternalNoNamespaceSchemaLocation(aXSDFilePath.c_str());

Why do you use setExternaNoNamespaceSchemaLocation? This API must be 
used for XML+XSD that don't use namespaces. In your case, as you are 
using the namespace "http://www.mywebsite.com/schema/myschema, you 
should invoke 
setExternalSchemaLocation(("http://www.mywebsite.com/schema/myschema 
"+aXSDFilePath).c_str())

Alberto

>      
>      aParser->setDoNamespaces(true);
>      aParser->setDoSchema(true);
>      aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
>      
>      aParser->parse(aInputSource);
>      xercesc::DOMDocument* aDocument = aParser->getDocument();
>      if (!aErrorHandler.HasErrorOccurred())
>      {
>        std::cout << "--- PARSE WORKED ---" << std::endl;
>        // do more things
>      }
>      else
>      {
>        std::cout <<"--- PARSE ERROR ---" << std::endl
>          << aErrorHandler.GetErrorString().c_str() << std::endl
>          << "--- IN XML ---" << std::endl
>          << aXML.c_str() << std::endl;
>
>          /* relevant error handler code:
>            void CustomParserErrorHandler::HandleError(
>                const xercesc::SAXParseException &aException,
>                std::stringstream                &aStream)
>            {
>              aStream
>                << xercesc::XMLString::transcode(aException.getMessage())
>                << " at line " << aException.getLineNumber()
>                << ", char " << aException.getColumnNumber()
>                << " in file "
>                << xercesc::XMLString::transcode(aException.getSystemId())
>                << std::endl;
>            }
>          */
>      }
>    }
>    catch (const xercesc::XMLException& toCatch) {
>      std::cout << "ERROR" << std::endl;
>      return 1;
>    }
>
>    xercesc::XMLPlatformUtils::Terminate();
>    std::cout << "bye" << std::endl;
>    return 0;
> }
> // end schemasend.cc
>
> where “MySchemaSimple.xsd” is the first schema in my original email, and
> “MySchema.xsd” is the second (the XML strings are also the same).  I can
> send CustomParserErrorHandler.hh/cc if necessary but all that does is print
> out error messages, so I don’t imagine it is the issue.
>


Re: Different target namespace error

Posted by em <wh...@gmail.com>.
Jeroen N. Witmond <jnw <at> xs4all.nl> writes:

> 
> On 2015-08-14 21:25, em wrote:
> > Hi,
> > 
> > I've been struggling to resolve this issue for a week now and haven't 
> > gotten
> > any responses on StackOverflow
> >
(http://stackoverflow.com/questions/31971324/error-different-target-namespace-but-schema-validates-and-namespaces-match),
> > so I found this list.
> 
> I've tried to recreate your problem but failed for lack of information; 
> see below.
> 
> > (snip)
>
> Please provide the exact commands that demonstrate the above file and 
> schema are valid.
>
> 
> > (snip)
> 
> Please provide the exact commands that demonstrate the above file and 
> schema are NOT valid; that is, the commands that result in the error 
> message you quoted above,.
> 
> 

Thanks for the reply, and apologies for lack of information. I just used an
online tool to verify my schema and XML:
http://www.utilities-online.info/xsdvalidation/ since I am new to XSD /
Xerces. The process for validating is inside of a rather complicated program
so I had to pare it down quite a bit.
Here is my C++ code:

// schemasend.cc, compile with:
// g++ -g -Wall -pedantic -L /usr/lib -o schemasend schemasend.cc
CustomParserErrorHandler.cc -lxerces-c

#include <xercesc/framework/MemBufInputSource.hpp>
#include "CustomParserErrorHandler.hh"
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
  xercesc::XMLPlatformUtils::Initialize();
  std::cout << "Intialized" << std::endl;
  try {
    // Parse document

    // This works:
    //const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\"
?><My_Data><PartOne>testing</PartOne><PartTwo>123</PartTwo></My_Data>";
    //static std::string aXSDFilePath("MySchemaSimple.xsd");
   
    const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\" ?><My_Data
xmlns=\"http://www.mywebsite.com/schema/myschema\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema\"><PartOne><ElemOne>thing</ElemOne></PartOne><PartTwo>123</PartTwo></My_Data>";
    static std::string aXSDFilePath("MySchema.xsd");
    
    CustomParserErrorHandler aErrorHandler;

    const xercesc::MemBufInputSource aInputSource(
      (const XMLByte*)aXML.c_str(), 
      static_cast<uint>(aXML.size()), 
      "stringBuffer");

    xercesc::XercesDOMParser* aParser = new xercesc::XercesDOMParser();
    aParser->cacheGrammarFromParse(true);

    aParser->setErrorHandler(&aErrorHandler);
    aParser->setDoNamespaces(true);
    aParser->setDoSchema(true);
    aParser->setValidationSchemaFullChecking(true);
    aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    aParser->setExternalNoNamespaceSchemaLocation((char*)NULL);
    aParser->setExternalNoNamespaceSchemaLocation(aXSDFilePath.c_str());
    
    aParser->setDoNamespaces(true);
    aParser->setDoSchema(true);
    aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    
    aParser->parse(aInputSource);
    xercesc::DOMDocument* aDocument = aParser->getDocument();
    if (!aErrorHandler.HasErrorOccurred())
    {
      std::cout << "--- PARSE WORKED ---" << std::endl;
      // do more things
    }
    else
    {
      std::cout <<"--- PARSE ERROR ---" << std::endl
        << aErrorHandler.GetErrorString().c_str() << std::endl
        << "--- IN XML ---" << std::endl
        << aXML.c_str() << std::endl;

        /* relevant error handler code:
          void CustomParserErrorHandler::HandleError(
              const xercesc::SAXParseException &aException,
              std::stringstream                &aStream)
          {
            aStream
              << xercesc::XMLString::transcode(aException.getMessage())
              << " at line " << aException.getLineNumber()
              << ", char " << aException.getColumnNumber()
              << " in file " 
              << xercesc::XMLString::transcode(aException.getSystemId())
              << std::endl;
          }
        */
    }
  }
  catch (const xercesc::XMLException& toCatch) {
    std::cout << "ERROR" << std::endl;
    return 1;
  }

  xercesc::XMLPlatformUtils::Terminate();
  std::cout << "bye" << std::endl;
  return 0;
}
// end schemasend.cc

where “MySchemaSimple.xsd” is the first schema in my original email, and
“MySchema.xsd” is the second (the XML strings are also the same).  I can
send CustomParserErrorHandler.hh/cc if necessary but all that does is print
out error messages, so I don’t imagine it is the issue.


Re: Different target namespace error

Posted by "Jeroen N. Witmond" <jn...@xs4all.nl>.
On 2015-08-14 21:25, em wrote:
> Hi,
> 
> I've been struggling to resolve this issue for a week now and haven't 
> gotten
> any responses on StackOverflow
> (http://stackoverflow.com/questions/31971324/error-different-target-namespace-but-schema-validates-and-namespaces-match),
> so I found this list.

I've tried to recreate your problem but failed for lack of information; 
see below.

> 
> I managed to reduce my original complex files down to shorter examples 
> which
> demonstrate the issue. I'm able to validate this XML:
> 
> <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
> <My_Data>
>     <PartOne>
>         testing
>     </PartOne>
>     <PartTwo>
>         123
>     </PartTwo>
> </My_Data>
> 
> with this XSD:
> 
> <xs:schema
>     xmlns:xs="http://www.w3.org/2001/XMLSchema"
>     elementFormDefault="qualified">
>     <xs:element name="My_Data">
>         <xs:complexType>
>             <xs:sequence>
>                 <xs:element name="PartOne" type="xs:string" 
> minOccurs="0"
> maxOccurs="1"/>
>                 <xs:element name="PartTwo" type="xs:integer" 
> minOccurs="0"
> maxOccurs="unbounded"/>
>             </xs:sequence>
>         </xs:complexType>
>     </xs:element>
> </xs:schema>

Please provide the exact commands that demonstrate the above file and 
schema are valid.

> 
> However, as soon as I add a targetNamespace attribute in the schema, I 
> get
> the error "has a different target namespace from the one specified in 
> the
> instance document ."
> 
> XML:
> 
> <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
> <My_Data
>     xmlns="http://www.mywebsite.com/schema/myschema"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema">
>     <PartOne>
>         <ElemOne>thing</ElemOne>
>     </PartOne>
>     <PartTwo>123</PartTwo>
> </My_Data>
> 
> XSD:
> 
> <xs:schema
>         xmlns="http://www.mywebsite.com/schema/myschema"
>         xmlns:xs="http://www.w3.org/2001/XMLSchema"
>         elementFormDefault="qualified"
>         attributeFormDefault="unqualified"
>         targetNamespace="http://www.mywebsite.com/schema/myschema">
>     <xs:element name="My_Data">
>         <xs:complexType>
>             <xs:sequence>
>                 <xs:element name="PartOne" type="SubType" minOccurs="0"
> maxOccurs="1"/>
>                 <xs:element name="PartTwo" type="xs:integer" 
> minOccurs="0"
> maxOccurs="unbounded"/>
>             </xs:sequence>
>         </xs:complexType>
>     </xs:element>
>     <xs:complexType name="SubType">
>         <xs:sequence>
>             <xs:element name="ElemOne" type="xs:string" />
>         </xs:sequence>
>     </xs:complexType>
> </xs:schema>

Please provide the exact commands that demonstrate the above file and 
schema are NOT valid; that is, the commands that result in the error 
message you quoted above,.

> 
> But the XML and XSD validate! Why would this be? I've tried adding a
> schemaLocation attribute and searched for other examples, but can't 
> figure
> out what is going on here.
> 
> Any insight would be greatly appreciated!
> 
> Thanks,
> 
> em