|
//============================================================================
|
|
// Name : SaxonCValidator.cpp
|
|
// Author :
|
|
// Version :
|
|
// Copyright : Your copyright notice
|
|
// Description : Hello World in C++, Ansi-style
|
|
//============================================================================
|
|
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "SaxonProcessor.h"
|
|
#include "XdmNode.h"
|
|
|
|
int main(int argc, char *argv [])
|
|
{
|
|
const char *xml =
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"
|
|
"<Dimensions>"
|
|
"<Length value=\"137.6\" units=\"in\" />"
|
|
"</Dimensions>";
|
|
|
|
const char *schema =
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
|
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
|
|
"<xs:simpleType name=\"units-type\">"
|
|
"<xs:restriction base=\"xs:string\">"
|
|
"<xs:pattern value=\"\\S+\" />"
|
|
"</xs:restriction>"
|
|
"</xs:simpleType>"
|
|
"<xs:simpleType name=\"non-negative-decimal\">"
|
|
"<xs:restriction base=\"xs:decimal\">"
|
|
"<xs:minInclusive value=\"0\"/>"
|
|
"</xs:restriction>"
|
|
"</xs:simpleType>"
|
|
"<xs:element name=\"Dimensions\">"
|
|
"<xs:complexType>"
|
|
"<xs:sequence>"
|
|
"<xs:element name=\"Length\" minOccurs=\"1\" maxOccurs=\"1\">"
|
|
"<xs:complexType>"
|
|
"<xs:attribute name=\"value\" type=\"non-negative-decimal\" use=\"required\" />"
|
|
"<xs:attribute name=\"units\" type=\"units-type\" use=\"required\" />"
|
|
" </xs:complexType>"
|
|
"</xs:element>"
|
|
"<xs:element name=\"Width\" minOccurs=\"0\" maxOccurs=\"1\">"
|
|
"<xs:complexType>"
|
|
"<xs:attribute name=\"value\" type=\"non-negative-decimal\" use=\"required\" />"
|
|
"<xs:attribute name=\"units\" type=\"units-type\" use=\"required\" />"
|
|
"</xs:complexType>"
|
|
"</xs:element>"
|
|
"<xs:element name=\"Depth\" minOccurs=\"0\" maxOccurs=\"1\">"
|
|
"<xs:complexType>"
|
|
"<xs:attribute name=\"value\" type=\"non-negative-decimal\" use=\"required\" />"
|
|
"<xs:attribute name=\"units\" type=\"units-type\" use=\"required\" />"
|
|
"</xs:complexType>"
|
|
"</xs:element>"
|
|
"</xs:sequence>"
|
|
"</xs:complexType>"
|
|
"</xs:element>"
|
|
"</xs:schema>";
|
|
|
|
std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!
|
|
|
|
SaxonProcessor *processor = new SaxonProcessor(true);
|
|
|
|
if (processor != nullptr)
|
|
{
|
|
processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation",
|
|
"/usr/lib/saxon-j/saxon-license.lic");
|
|
|
|
SchemaValidator *validator = nullptr;
|
|
XdmNode * input = nullptr;
|
|
|
|
try
|
|
{
|
|
validator = processor->newSchemaValidator();
|
|
|
|
if (validator != nullptr)
|
|
{
|
|
validator->registerSchemaFromString(schema);
|
|
|
|
input = processor->parseXmlFromString(xml);
|
|
|
|
if (input != nullptr)
|
|
{
|
|
validator->setSourceNode(input);
|
|
validator->validate();
|
|
|
|
printf("Validation successful!\n");
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Call to SaxonProcessor::parseXmlFromString() failed\n");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Could not create SchemaValidator\n");
|
|
}
|
|
}
|
|
catch(SaxonApiException& e)
|
|
{
|
|
fprintf(stderr, "Caught exception: %s\n", e.what());
|
|
}
|
|
|
|
if (validator != nullptr)
|
|
{
|
|
delete validator;
|
|
}
|
|
|
|
if (input != nullptr)
|
|
{
|
|
delete input;
|
|
}
|
|
|
|
delete processor;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Could not create SaxonProcessor\n");
|
|
}
|
|
|
|
SaxonProcessor::release();
|
|
|
|
return 0;
|
|
}
|
|
|