[xml] DTD Validation Problem



Hi,

I tried to dig up a lot of information but couldn't find a problem that
I'm facing. I'm probably missing something really small but can't seem
to figure it out.

Here's my problem:

I'm trying to validate a XML document via a DTD. The XML and DTD are
located on different locations (but on the same machine, no network is
involved). For example, let's say the following are the locations on my
machine for XML and DTD respectively:

XML: c:\temp\xml\xml1.xml
DTD: c:\temp\dtd\xml1.dtd

Let me point out, the following code DOES validate the XML document only
if the DTD is in the directory where I'm executing my test program from.
For example, my test program is in C:\temp\test\test1.exe. Then, if I
put the DTD (xml1.dtd) in c:\temp\test\ it works. But if I give the full
location (including the path) of the DTD file then it doesn't. I've
tried URI also for DTD parameter, 'file:///c:/temp/dtd/xml1.dtd'. That
didn't work either. I'm possibly missing something really minor here.
Can anyone suggest what is it? I'm sending the code snippet. BTW, you
must have figured out, I'm on Windows. I'm using build libxml2-2.6.10.

---------------------------
static void dtdTest(char * xmlFile, char * dtdFile)
{
    xmlDocPtr       pDoc    = NULL;
    xmlDtdPtr       pDtd    = NULL;
    xmlValidCtxtPtr pCtxt   = NULL;
    int             iStatus = 1;

    // Create doc pointer
    pDoc = xmlReadFile(xmlFile, NULL, 0);
    if ( NULL == pDoc ) iStatus = 0; 

    // create dtd pointer
    if ( iStatus )
    {
        pDtd = xmlParseDTD(NULL, (const xmlChar*)dtdFile);
        if ( NULL == pDtd ) iStatus = 0;
    }

    // Create new context
    if ( iStatus )
    {
        pCtxt = xmlNewValidCtxt();
        if ( NULL == pCtxt ) iStatus = 0;
    }

    if ( iStatus )
    {
        iStatus = xmlValidateDtd( pCtxt, pDoc, pDtd );    
        if ( iStatus ) 
            printf("Validation Succeeded\n");
        else  
            printf("Validation Failed\n");
    }

    // clean memory.
    if ( pDoc ) xmlFreeDoc (pDoc);
    if ( pDtd ) xmlFreeDtd (pDtd);
    if ( pCtxt ) xmlFreeValidCtxt( pCtxt );
    return;
}
--------

Sameer 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]