Beispiel #1
0
        public static void validation()
        {
            Saxon.Api.Processor proc = new Processor(true);

            SchemaManager schemaManager = proc.SchemaManager;

            FileStream xsdFs         = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Validation\\SAFTPT1.04_01.xsd", FileMode.Open);
            XmlReader  validationXml = XmlReader.Create(xsdFs);

            Boolean bSchema = schemaManager == null;
            Boolean bXml    = validationXml == null;

            //throw new Exception("debug: schema: " + bSchema + " xml: " + bXml);

            // XmlReaderSettings settings = new XmlReaderSettings();
            // settings.ValidationType = ValidationType.Schema;

            schemaManager.Compile(validationXml);
            SchemaValidator schemaValidator = schemaManager.NewSchemaValidator();

            FileStream xmlFs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Assets\\SAFT_DEMOSINF_01-01-2016_31-12-2016.xml", FileMode.Open);

            schemaValidator.SetSource(XmlReader.Create(xmlFs));

            schemaValidator.Run();
        }
        public bool Validate(string xml)
        {
            bool result = true;

            Saxon.Api.Processor proc          = new Processor(true);
            SchemaManager       schemaManager = proc.SchemaManager;

            schemaManager.Compile(XmlReader.Create(new StringReader(this.xsd)));
            SchemaValidator validator = schemaManager.NewSchemaValidator();

            validator.SetSource(XmlReader.Create(new StringReader(xml)));
            validator.ErrorList = new ArrayList();
            try
            {
                validator.Run();
            }
            catch (Exception)
            {
                result = false;
            }
            return(result);
        }
Beispiel #3
0
    /**
     * Method main. First argument is the Saxon samples directory.
     */
    public static void Main(String[] argv)
    {
        String samplesDir;

        if (argv.Length > 0)
        {
            samplesDir = argv[0];
        }
        else
        {
            String home = Environment.GetEnvironmentVariable("SAXON_HOME");
            if (home == null)
            {
                Console.WriteLine("No input directory supplied, and SAXON_HOME is not set");
                return;
            }
            else
            {
                if (home.EndsWith("/") || home.EndsWith("\\"))
                {
                    samplesDir = home + "samples/";
                }
                else
                {
                    samplesDir = home + "/samples/";
                }
            }
        }

        UriBuilder ub = new UriBuilder();

        ub.Scheme = "file";
        ub.Host   = "";
        ub.Path   = samplesDir;
        Uri baseUri = ub.Uri;

        Console.WriteLine("Base URI: " + baseUri.ToString());

        // Create a schema-aware Processor

        Processor saxon = new Processor(true);

        // Load a schema

        SchemaManager manager = saxon.SchemaManager;

        manager.ErrorList = new ArrayList();
        Uri schemaUri = new Uri(baseUri, "data/books.xsd");

        try {
            manager.Compile(schemaUri);
        } catch (Exception e) {
            Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors");
            foreach (StaticError error in manager.ErrorList)
            {
                Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
            }
            return;
        }


        // Use this to validate an instance document

        SchemaValidator validator   = manager.NewSchemaValidator();
        Uri             instanceUri = new Uri(baseUri, "data/books.xml");

        validator.SetSource(instanceUri);
        validator.ErrorList = new ArrayList();
        XdmDestination psvi = new XdmDestination();

        validator.SetDestination(psvi);

        try {
            validator.Run();
        } catch (Exception e) {
            Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
            foreach (StaticError error in validator.ErrorList)
            {
                Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
            }
        }


        // Run a query on the result to check that it has type annotations

        XQueryCompiler  xq = saxon.NewXQueryCompiler();
        XQueryEvaluator xv = xq.Compile("data((//PRICE)[1]) instance of xs:decimal").Load();

        xv.ContextItem = psvi.XdmNode;
        Console.WriteLine("Price is decimal? " + xv.EvaluateSingle().ToString());
    }