Issues with Using count() and normalize-space() Functions in XPath with XMLutils in Igor Pro
I am parsing an XML file using [XMLutils](https://github.com/IP-XOP/XMLutils) in Igor Pro, but I am encountering issues with the `count()` function, which is used to count nodes in a node-set, and the `normalize-space()` function. These functions do not behave as expected in my code. I suspect that I might be using them incorrectly.
I have verified that the XPath expressions work as expected in `xmllint`, as shown below:
$ xmllint --xpath "/modeling/kpoints/varray[@name='kpointlist']/v" test.xml
<v> 0.00000000 0.00000000 0.00000000 </v>
<v> 0.05000000 0.00000000 0.05000000 </v>
<v> 0.10000000 0.00000000 0.10000000 </v>
<v> 0.15000000 0.00000000 0.15000000 </v>
<v> 0.20000000 0.00000000 0.20000000 </v>
<v> 0.25000000 0.00000000 0.25000000 </v>
<v> 0.30000000 0.00000000 0.30000000 </v>
<v> 0.35000000 0.00000000 0.35000000 </v>
<v> 0.40000000 0.00000000 0.40000000 </v>
<v> 0.45000000 0.00000000 0.45000000 </v>
<v> 0.50000000 0.00000000 0.50000000 </v>
<v> 0.50000000 0.00000000 0.50000000 </v>
<v> 0.50000000 0.02500000 0.52500000 </v>
<v> 0.50000000 0.05000000 0.55000000 </v>
<v> 0.50000000 0.07500000 0.57500000 </v>
<v> 0.50000000 0.10000000 0.60000000 </v>
<v> 0.50000000 0.12500000 0.62500000 </v>
<v> 0.50000000 0.15000000 0.65000000 </v>
<v> 0.50000000 0.17500000 0.67500000 </v>
<v> 0.50000000 0.20000000 0.70000000 </v>
$ xmllint --xpath "count(/modeling/kpoints/varray[@name='kpointlist']/v)" test.xml
20
$ xmllint --xpath "normalize-space(/modeling/kpoints/varray[@name='kpointlist']/v[1])" test.xml
0.00000000 0.00000000 0.00000000
However, when I try using these functions in Igor Pro with XMLutils, I get no results, even though there are no errors. Below is the relevant code:
Function parseXmlDemo1()
String xmlFile
String XPathStr
String nameSpaceStr
String delimiterStr
Variable fileID
xmlFile="C:test.xml"
fileID = xmlopenfile(xmlFile)
nameSpaceStr = ""
delimiterStr = "\r\n"
// It works as expected
XPathStr = "/modeling/kpoints/varray[@name='kpointlist']/v"
print XMLstrFmXpath(fileID,XPathStr,nameSpaceStr,"")
XMLwaveFmXpath(fileID,XPathStr,nameSpaceStr,delimiterStr) // ==> M_xmlcontent, W_xmlcontentnodes
xmlclosefile(fileID,0)
End
Function parseXmlDemo2()
String xmlFile
String XPathStr
String nameSpaceStr
String delimiterStr
Variable fileID
xmlFile="C:test.xml"
fileID = xmlopenfile(xmlFile)
nameSpaceStr = ""
delimiterStr = "\r\n"
// No erros but empty results
XPathStr = "count(/modeling/kpoints/varray[@name='kpointlist']/v)"
print XMLstrFmXpath(fileID,XPathStr,nameSpaceStr,"") // nothing
XMLwaveFmXpath(fileID,XPathStr,nameSpaceStr,delimiterStr) // nothing
xmlclosefile(fileID,0)
End
Function parseXmlDemo3()
String xmlFile
String XPathStr
String nameSpaceStr
String delimiterStr
Variable fileID
xmlFile="C:test.xml"
fileID = xmlopenfile(xmlFile)
nameSpaceStr = ""
delimiterStr = "\r\n"
// No erros but empty results
XPathStr = "normalize-space(/modeling/kpoints/varray[@name='kpointlist']/v[1])"
print XMLstrFmXpath(fileID,XPathStr,nameSpaceStr,"") // nothing
XMLwaveFmXpath(fileID,XPathStr,nameSpaceStr,delimiterStr) // nothing
xmlclosefile(fileID,0)
End
I suspect that the functions `count()` and `normalize-space()` should work in Igor Pro using XMLutils, as both `xmllint` and XMLutils will return errors if an unknown function is used (see below):
$ xmllint --xpath "Unknown(/modeling/kpoints/varray[@name='kpointlist']/v)" test.xml
xmlXPathCompOpEval: function Unknown not found
XPath error : Unregistered function
XPath evaluation failure
Function parseXmlDemo4()
String xmlFile
String XPathStr
String nameSpaceStr
String delimiterStr
Variable fileID
xmlFile="C:test.xml"
fileID = xmlopenfile(xmlFile)
nameSpaceStr = ""
delimiterStr = "\r\n"
// Define an invalid XPath expression
// Note: The function "Unknown" does not exist, so this will result in an error
// Expected error message: "XMLutils: Failure with XPath expression"
XPathStr = "Unknown(/modeling/kpoints/varray[@name='kpointlist']/v[1])"
print XMLstrFmXpath(fileID,XPathStr,nameSpaceStr,"") // nothing
XMLwaveFmXpath(fileID,XPathStr,nameSpaceStr,delimiterStr) // nothing
xmlclosefile(fileID,0)
End
Has anyone else experienced similar issues or know how to make the `count()` and `normalize-space()` functions work correctly with XMLutils in Igor Pro? A simplified XML file, used to demonstrate this issue, is attached.
I don't think that'll work. You should be able to get a wave of all the successful wavefmxpath entries, then work out the length of that.
March 3, 2025 at 10:46 pm - Permalink
In reply to I don't think that'll work… by andyfaff
Yes. Thank you! It is a possible way.
May 7, 2025 at 12:56 am - Permalink