Page 1 of 1

LibXML2 Static XPath Test Crossplatform

Posted: Thu Oct 31, 2024 10:10 pm
by Justin
I compiled libxml2 statically for Windows to test the XPath support, it may be useful if someone needs xpath.
I disabled libiconv wich was LGPL so now everything should be MIT.

The project is at github, simply run xpathTest.pb, it's a very simple xpath test, just a proof of concept:
https://github.com/omegakode/PBLibXML2

LibXML2 is crossplatform Win64, Linux and MacOS supported.

Linux:
sudo apt-get install libxml2-dev

MacOS:
Install MacPorts
https://www.macports.org/
And:
sudo port install libxml2

LibXML2 source if you want to compile it by yourself:
https://github.com/GNOME/libxml2

LibXML2 manual:
https://gnome.pages.gitlab.gnome.org/li ... index.html

XPath 1.0
https://www.w3.org/TR/1999/REC-xpath-19991116/

The xpathTest.pb example:

Code: Select all

;xpathTest.pb

EnableExplicit

IncludeFile "libxml2.pbi"

Procedure main()
	Protected.xmlXPathContext *xpCtx
	Protected.xmlDoc *doc
	Protected.s xml, xp
	Protected.xmlXPathObject *xpObj
	Protected.l i, sz
	Protected.xmlNodeSet *nodeSet
	Protected.xmlNode *node
	Protected.i attVal
	Protected.xmlAttr *attr
	
	xmlInitParser()
	
	xml = "test3.xml"
	*doc = xmlParseFile(xml)
	If *doc = 0 : ProcedureReturn : EndIf 
	
	*xpCtx = xmlXPathNewContext(*doc)
	If *xpCtx = 0 : ProcedureReturn : EndIf 

	xp = "//child2"
	*xpObj = xmlXPathEvalExpression(xp, *xpCtx)
	If *xpObj = 0 : ProcedureReturn : EndIf 

	*nodeSet = *xpObj\nodesetval
	If *nodeSet = 0 : ProcedureReturn : EndIf 

	sz = *nodeSet\nodeNr	
	For i = 0 To sz - 1
		*node = *nodeSet\nodeTab\node[i]
		
		If *node\type = #XML_ELEMENT_NODE
			If *node\name
				Debug PeekS(*node\name, -1, #PB_UTF8)
			EndIf
			
			;Attributes
			*attr = *node\properties
			While *attr
				Debug "Attr:"
				If *attr\name
					Debug PeekS(*attr\name, -1, #PB_UTF8)
				EndIf
				
				attVal = xmlNodeListGetString(*node\doc, *attr\children, 1)
				If attVal
					Debug PeekS(attVal, -1, #PB_UTF8)
					xmlMemFree(attVal)
				EndIf
				
				*attr = *attr\next
			Wend 
		Else
		
		EndIf
	Next 
	
	xmlXPathFreeObject(*xpObj);
	xmlXPathFreeContext(*xpCtx); 
	xmlFreeDoc(*doc)
EndProcedure

main()

Re: LibXML2 Static XPath test

Posted: Thu Oct 31, 2024 10:33 pm
by idle
That looks really useful.

Re: LibXML2 Static XPath test

Posted: Fri Nov 01, 2024 11:45 am
by Justin
Updated for Linux, if you get some error use:
sudo apt-get install libxml2-dev

Re: LibXML2 Static XPath Test Crossplatform

Posted: Fri Nov 01, 2024 4:00 pm
by Justin
Updated for MacOS, now is crossplatform.

Install MacPorts
https://www.macports.org/
And:
sudo port install libxml2