• C

Use libxml2 under linux to validate xpath against schema

Hello,

I have a question regarding programming libxml2 in C under linux.

Assume an XSD file called test.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="test" type="Test-Type"/>
    <xs:complexType name="Test-Type">
        <xs:all>
            <xs:element name="test-type-1"  type="xs:string"/>
            <xs:element name="test-type-2"  type="xs:integer"/>
        </xs:all>
    </xs:complexType>
</xs:schema>

And an XML file called test.xml:

<test>
  <test-type-1>foo</test-type-1>
  <test-type-2>1</test-type-2>
</test>


We can verify that this schema validates like this:
$ xmllint --noout --schema test.xsd test.xml
test.xml validates


It is trivial to write a program to validate an entire XML file against a schema.  For instance, if I can use this snippet:

  ctxt = xmlSchemaNewParserCtxt("test.xsd");
  schema=xmlSchemaParse(ctxt);

To verify the XSD file and then:
   
  doc = xmlReadMemory (xml_buf, strlen(xml_buf), NULL, NULL, 0);
  schemaErrors = xmlSchemaValidateDoc(ctxt, doc);
 
to validate the xml file against the XSD schema.  

So my question:  what if I want to validate not an ENTIRE xml document but only PART of one?  
I'd like to only check an xpath -- not an entire XML document -- for validation.  For instance, have a buffer containing:

"<test-type-2>10</test-type-2"

and validate that against the test.xsd file.  So if the buffer contained
"<test-type-2>foo</test-type-2>"

receive the libxml2 error message:

"Schemas validity error : Element 'test-type-2': 'foo' is not a valid value of the atomic type 'xs:integer'.
test.xml fails to validate"


I think the libxml2 function xmlSchemaValidateOneElement() is what I need to use, but I can't get it work.

Any thoughts or ideas?

Thanks!



pennypiperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
If I recall correctly, xmlSchemaValidateOneElement starts validating the subtree starting from the given node. However, that given node has to be a valid root node for the schema. In your case, it has to be a 'test' node.

I'm not aware of a way to do what you want in libxml2 though ...
0
Geert BormansInformation ArchitectCommented:
I am not sure if this is what Infinity08 is saying, but...

For what I know xmlSchemaValidateOneElement() is what you should use.
With xmlSchemaValidateOneElement one has the freedom to choose the
validation root node. But bear in mind that all other constraining aspects of XML Schema
validation apply: i.e. the validation root must have a global element declaration.

A globally defined element is one that is defined as a direct child of the xs:schema element
and referenced inside a complexType (sequence, choice or whatever) with an ref attribute

<xs:element name="order">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="TimeDate"/>
            <xs:element name="ProMember"/>
         </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="TimeDate"/>

In this example, <TimeDate> and <order> are global elements (and can serve as a root node for validation purposes)
<ProMember> is a locally defined element and can only live inside the context of an order. So you can't validate just <ProMember>
with xmlSchemaValidateOneElement(), you would need to validate at least up to the level of <order>

If you want to validate <ProMember> elements, you will need to change the schema and move the <ProMember> definition outside the <order> content type.

If I now look back at your example, I see that <test-type-1> and <test-type-2>
are localy declared... meaning they can't be used as a valid root node.
So you definitely have to start by changing that.

I hope this makes sense

Geert
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pennypiperAuthor Commented:
Thanks Geert.
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.