?
Solved

how to Validate partial XML

Posted on 2010-01-08
5
Medium Priority
?
637 Views
Last Modified: 2012-05-08
Hi,

I need to validate my XML partially. I will explain my question by taking example. suppose i have XML like
<Columns>
  <Obj></Obj>
  <TYPE></TYPE>
  <FEATURE_ID></FEATURE_ID>
  <LOCATION></LOCATION>
  <UNIT></UNIT>
</Columns>

now i have to validate for node Feature_ID, I don't want to validate rest of the nodes. They may appear or not. I wrote XSD as

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Columns">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="FEATURE_ID" minOccurs ="1" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

but this is giving me error that "Obj" element is present in the XML. How to do that?

Anurag
0
Comment
Question by:Anurag Agarwal
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26308242
There is no way I can think of to make this work in a schema without help from schematron (www.schematron.com) or assertions in schema1.1
See if you can merge schematron rules in your particular parser, or see if your particular parser supports schema1.1 (Saxon EE and Xerces-J do)

Here is a schema, that requires that colums exists of a number of unknown elements, validated in a lax fashion.
That means that if an element is encountered that is known by the schema, it needs to be valid according to the schema
(in this example FEATURE_ID needs to contain an integer... for the sake of the example)

The XPath for the schematron assertion would be count(FEATURE_ID) = 1 to guarantuee that there is only one FEATURE_ID element in the sequence
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Columns">
        <xs:complexType>
            <xs:sequence>
                <xs:any processContents="lax" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="FEATURE_ID" type="xs:integer"></xs:element>
</xs:schema>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26308275
here is a schema 1.1 schema with the assertion built in
(only works on parsers that support schema 1.1)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Columns">
        <xs:complexType>
            <xs:sequence>
                <xs:any processContents="lax" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:assert test="count(FEATURE_ID) = 1"/>
    </xs:element>
    <xs:element name="FEATURE_ID" type="xs:integer"></xs:element>
</xs:schema> 

Open in new window

0
 
LVL 11

Author Comment

by:Anurag Agarwal
ID: 26313424
I am using this in Microsoft .Net environment and i guess this doesn't support schema 1.1
 
Anurag
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1500 total points
ID: 26313586
well, I am not sure about that.
Try the above schema and see how msxml behaves
There is a .net variant of saxon (www.saxonica.com) if you want
you can check that out if you want

the other option is using schematron with it
and there must be ways to validate with schematron in .net
one I can think of is a project schematron.net
http://msdn.microsoft.com/en-us/library/aa468554.aspx#schematron_topic4
0
 
LVL 11

Author Closing Comment

by:Anurag Agarwal
ID: 31674501
I can not use schematron as i am working on one product and for this small problem we don't want to add one more extra dependency, so i will change my approach.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question