Use libxml2 under linux to validate xpath against schema

Posted on 2008-10-16
Last Modified: 2010-04-21

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="">
    <xs:element name="test" type="Test-Type"/>
    <xs:complexType name="Test-Type">
            <xs:element name="test-type-1"  type="xs:string"/>
            <xs:element name="test-type-2"  type="xs:integer"/>

And an XML file called test.xml:


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");

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:


and validate that against the test.xsd file.  So if the buffer contained

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?


Question by:pennypiper
  • 2
LVL 53

Expert Comment

Comment Utility
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 ...
LVL 60

Accepted Solution

Geert Bormans earned 500 total points
Comment Utility
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:element ref="TimeDate"/>
            <xs:element name="ProMember"/>

<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


Author Closing Comment

Comment Utility
Thanks Geert.
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now