Hi,

Please help me regarding a query on streaming XPath.

The version of libxml2 and the OS on which it is deployed is displayed
below.

-----------------------------------------------------------------------
shell$ xml2-config --version
2.6.13

shell$ cat /proc/version
Linux version 2.6.9-1.667smp ([EMAIL PROTECTED]) (gcc
version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)) #1 SMP Tue Nov 2 14:59:52
EST 2004
-----------------------------------------------------------------------

I have attached a code sample, which uses streaming XPath using
xmlReader and xmlPattern. The inputs to this program are:

1). A XML message, which is well-formed, would be passed to this code
fragment for XPath matching.
2). A XPath expression.

Could you please review the code and let me know your feedback?

There are two issues. Please clarify.

1). The webpage shows an enum for the XML pattern flags.

Enum xmlPatternFlags {
    XML_PATTERN_DEFAULT = 0 : simple pattern match
    XML_PATTERN_XPATH = 1 : standard XPath pattern
    XML_PATTERN_XSSEL = 2 : XPath subset for schema selector
    XML_PATTERN_XSFIELD = 4 : XPath subset for schema field
}

Using 'XML_PATTERN_XPATH' results in compilation error.
Search for this string in the code base failed, which implies 
that it is not supported.

So currently I am passing a value of '1'.

2). The function "xmlPatternCompile" seems to compile valid and invalid
XPaths.

-----------------------------------------------------------------------------
      patternc = xmlPatterncompile(
                    (const xmlChar *) pattern,
                    NULL,
                    1,
                    NULL
      );
-----------------------------------------------------------------------------

Could you please provide me a few invalid XPaths to test my code?
I assume that invalid XPath would result in a NULL return.

Thanks a lot for your help.

Regards,
Ravi.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/debugXML.h>
#include <libxml/xmlerror.h>
#include <libxml/globals.h>
#include <libxml/xmlreader.h>
#include <libxml/pattern.h>

#define BUFFER_SIZE  128

int main(int argc, char **argv)
{
   FILE *xmlFile;
   char *filename = NULL;
   const char *pattern = NULL;
   xmlPatternPtr patternc = NULL;
   xmlTextReaderPtr reader;
   int options = 0;
   
   int readlen;
   char buffer[BUFFER_SIZE];

   if (argc != 3)
   {
      printf("Usage : %s <XML file> <XPath pattern>\n", argv[0]);
      return -1;
   }

   LIBXML_TEST_VERSION;

   pattern = argv[2];

   if (pattern != NULL) 
   {
      patternc = xmlPatterncompile(
                    (const xmlChar *) pattern, 
                    NULL, 
                    1,
                    NULL
      );

      if (patternc == NULL) 
      {
         xmlGenericError(
            xmlGenericErrorContext,
            "Pattern %s failed to compile\n", 
            pattern
         );
         pattern = NULL;

         printf("Pattern %s failed to compile\n", pattern);

         return -5;
      }
   }

   filename = argv[1];

   if ((xmlFile = fopen(filename , "r")) == NULL)
   {
      printf("Not able to open file %s\n", filename);
      return -2;
   }

   options |= XML_PARSE_NOERROR;
   options |= XML_PARSE_NOWARNING;

   do
   {
      int ret;

      readlen = fread(buffer, 1, BUFFER_SIZE, xmlFile);

      reader = xmlReaderForMemory(
               buffer,
               readlen,
               filename,
               NULL,
               options
      );

      if (reader == NULL)
      {
         printf("Reader is NULL\n");
         return -3;
      }

      ret = xmlTextReaderRead(reader);

      //printf("==============\n");
      //printf("Buffer is %s\n", buffer);
      //printf("==============\n");

      while (ret == 1) 
      {
          const xmlChar *name, *value;
          
          name = xmlTextReaderConstName(reader);

          if (name == NULL)
          {
             name = BAD_CAST "--";
          }

          value = xmlTextReaderConstValue(reader);

          printf("%d %d %s %d %d",
                  xmlTextReaderDepth(reader),
                  xmlTextReaderNodeType(reader),
                  name,
                  xmlTextReaderIsEmptyElement(reader),
                  xmlTextReaderHasValue(reader)
          );

          if (value == NULL)
          {
             printf("\n");
          }
          else 
          {
             printf(" %s\n", value);
          }

          if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) 
          {
             printf("Node matches pattern %s\n", pattern);
          }

          ret = xmlTextReaderRead(reader);
      }

      xmlFreeTextReader(reader);
   } while (readlen == BUFFER_SIZE);

   xmlFreePattern(patternc);

   xmlCleanupParser();

   xmlMemoryDump();

   fclose(xmlFile);

   return 0;
}
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to