Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How to do Schematron validation over different xml files attributes

$
0
0
I have one source1.xml file 

    <?xml version="1.0" encoding="utf-8" ?>
    <list>
      <author id="a1">Alice</author>
      <author id="p1">Paul</author>
      <author id="p2">Peter</author>
    </list>

another source2.xml 

    <?xml version="1.0" encoding="utf-8" ?>
    <list>
      <author id="1"/>
      <author id="a2"/>
      <author id="p1"/>
    </list>

I need to validate "id" attribute of the two xml files are having same values or not.
I can do it using the schematron as shown in this link ( http://zvon.org/xxl/SchematronTutorial/Examples/Example17/example.html)

So I have created on xsd file (source2.xsd) where my schematron is included as shown below 

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="list">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" name="author">
    
              <xs:annotation>
                <xs:appinfo>
                  <sch:pattern name ="Compare AdaptiveDisplayDevice Id with DisplayDevice Id" xmlns:sch="http://www.ascc.net/xml/schematron">
                   
                    <rule context="author">
                      <report test="document('source1.xml')//author[@id=current()/@id]">
                        Atrribute
                        <name path="document('source1.xml')//author[@id=current()/@id]"/> is forbidden in element
                        <name/>
                      </report>
                    </rule>
    
                  </sch:pattern>
                </xs:appinfo>
              </xs:annotation>
              
              <xs:complexType>
                <xs:attribute name="id" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

My C# code to validation is as shown below 

    using System.Text;
    using NMatrix.Schematron;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
    
                    /* setup schematron validation */
                    Validator validator = new Validator();
                    validator.AddSchema(@"D:\POC\ConsoleApplication1\ConsoleApplication1\source2.xsd");
    
                    /* run both validators at once */
                    validator.Validate(new XmlTextReader(@"D:\POC\ConsoleApplication1\ConsoleApplication1\source2.xml"));
     
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
    
            }
        } 
    }

If we observe two xml files , then while validation using schematron it should give me error at first two id values. However I am not getting any error ..
Can anyone help me here ....

Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>