[xml] Ask an XPath problem



Hi all:
        
        I want to get the attributes from the following Xml:
                <?xml version="1.0"?><Object Id="a123">test123<Word>Wahaha</Word></Object>
        So, I use the following Location Path trying to select the attributes. I expect to get Id, a123, but I
        get "<Object></Object>" instead. 
                (//. | //@* | //namespace::*)[attribute::*]
        
        My testing program is as follows. Could anyone tell me how to select all attribute nodes from the 
        context node? Thanks in advance.

        
int 
main()
{
        xmlDocPtr pXml = NULL ;
        xmlXPathContextPtr cTxt = NULL ;
        xmlXPathObjectPtr xpathObj = NULL ; 
        xmlNodeSetPtr pNodeSet = NULL ;
        xmlOutputBufferPtr pOutBuff = NULL ;
        char *pszExprTmpl = "(//. | //@* | //namespace::*)[%s]" ;
        char szXPathExpr[512] = {0} ;
        char *pXmlMem = "<?xml version=\"1.0\"?><Object Id=\"a123\">test123<Word>Wahaha</Word></Object>" ;
        int nXmlMemLen = strlen((char *)pXmlMem) ;

        pXml = xmlParseMemory(pXmlMem, nXmlMemLen) ;
        cTxt = xmlXPathNewContext(pXml) ;


        
        pOutBuff = xmlAllocOutputBuffer(NULL) ;
        
        sprintf(szXPathExpr, pszExprTmpl, "attribute::*") ;

        xpathObj = xmlXPathEval((xmlChar *)szXPathExpr, cTxt) ;
        
        xmlC14NDocSaveTo(pXml, xpathObj->nodesetval , 0, NULL, 0, pOutBuff) ;

        fprintf(stdout, "%s", pOutBuff->buffer->content) ;

        return 0 ;
}
        
        
                                                                kltsai
-- 





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