Skip to content
⌘ NSIS Forum Archive

nsisXML xpath count elements

3 posts

meoit#

nsisXML xpath count elements

Dear Sir,

nsisXML v1.4 2012-8-10 Wizou OlivierMarcoux

How to count element "HardDisk" in xml files ?

<?xml version="1.0"?>
<MemuHyperv xmlns="http://www.memuhyperv.org/" version="1.2-windows">
<Machine uuid="{20210808-aaaa-aaaa-aaaa-000000000000}" lastStateChange="2021-08-08">
<MediaRegistry>
<HardDisks>
<HardDisk uuid="{20210808-bbbb-bbbb-bbbb-000000000000}" location="disk1.vmdk" format="VHD" type="Normal"/>
<HardDisk uuid="{20210808-cccc-cccc-cccc-000000000000}" location="disk2.vmdk" format="VHD" type="Normal"/>
</HardDisks>
</MediaRegistry>
</Machine>
</MemuHyperv>

I need result = 2.
Anders#
In theory it would be something like nsisXML::select count(//MemuHyperv/Machine/MediaRegistry/HardDisks/HardDisk) but I could not get that to work.

​If I change your XML to

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MemuHyperv version="1.2-windows">
<Machine uuid="{20210808-aaaa-aaaa-aaaa-000000000000}" lastStateChange="2021-08-08">
<MediaRegistry>
<HardDisks>
<HardDisk uuid="{20210808-bbbb-bbbb-bbbb-000000000000}" location="disk1.vmdk" format="VHD" type="Normal"/>
<HardDisk uuid="{20210808-cccc-cccc-cccc-000000000000}" location="disk2.vmdk" format="VHD" type="Normal"/>
</HardDisks>
</MediaRegistry>
</Machine>
</MemuHyperv>​
Then this hack works
!include Logiclib.nsh
nsisXML::load "temp.xml"
StrCpy $9 1
loop:
nsisXML::select "//MemuHyperv/Machine/MediaRegistry/HardDisks/HardDisk[$9]"
${If} $1 P<> 0
nsisXML::release $1
IntOp $9 $9 + 1
StrCpy $1 0
StrCpy $2 0
goto loop
${EndIf}
IntOp $9 $9 - 1
MessageBox mb_ok "$9 elements"​ 
The select probably supports namespaces but my hate for XML is stronger than my desire to research it...
meoit#edited
Final, working.

But it's inconvenient, I have to run an additional command to find and delete "xmlns="http://www.memuhyperv.org/"".

<?xml version="1.0" ?>
<MemuHyperv version="1.2-windows">​

change ${If} $1 P<> 0 to ${If} $1 <> 0