Search code examples
installationnsis

Unable to update value of particular element if it is empty in xml using NSIS installer


I am working on NSIS [Nullsoft Installer System] Installer script. I am facing issue to update XML element value [used XML plugin] when element is empty for example,

**Element value to be updated: <Name />
After executing the below script, the output is : <Test />**

${xml::LoadFile} "$EXEDIR\install1.config" $0
${If} $0 != -1
  ${xml::GotoPath} "/InstallerInputs/Name" $0
  ${xml::FirstChild} "" $0 $1
  ${xml::SetNodeValue} "Test"
  ${xml::SaveFile} "$EXEDIR\install1.config" $0
  ${xml::Unload}
  ${EndIf}

XML structure is like below,

<?xml version="1.0" encoding="utf-16" ?>
<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   **<Name></Name>** or **<Name />**
   <Password></Password>
</InstallerInputs>

Output XML:

<?xml version="1.0" encoding="utf-16" ?>
<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   **<Test/>**
   <Password></Password>
</InstallerInputs>

Any help....


Solution

  • Two issues:

    1. After GotoPath you are already at the correct node, ${xml::FirstChild} will fail because Name has no children.
    2. ${xml::SetNodeValue} seems to set the tag name, not the inner text.

    Try this

    !if 0
    FileOpen $0 "$Temp\test.xml" w
    FileWrite $0 '<?xml version="1.0" encoding="utf-16" ?>$\r$\n'
    FileWrite $0 '<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">$\r$\n'
    FileWrite $0 '<Name></Name>$\r$\n'
    FileWrite $0 '<Password></Password>$\r$\n'
    FileWrite $0 '</InstallerInputs>$\r$\n'
    FileClose $0
    !endif
    
    ${xml::LoadFile} "$Temp\test.xml" $0
    ${If} $0 != -1
        ${xml::GotoPath} "/InstallerInputs/Name" $0
        ${If} $0 = 0
            ${xml::SetText}  "Test" $0
            ${xml::SaveFile} "$Temp\test.xml" $0
        ${EndIf}
        ${xml::Unload}
    ${EndIf}