簡體   English   中英

如果使用 NSIS 安裝程序在 xml 中為空,則無法更新特定元素的值

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

我正在研究 NSIS [Nullsoft 安裝程序系統] 安裝程序腳本。 例如,當元素為空時,我面臨更新 XML 元素值 [使用的 XML 插件] 的問題,

**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結構如下,

<?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>

輸出 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>

任何幫助....

兩個問題:

  1. 在 GotoPath 之后,您已經在正確的節點上, ${xml::FirstChild}將失敗,因為 Name 沒有子節點。
  2. ${xml::SetNodeValue}似乎設置標簽名稱,而不是內部文本。

嘗試這個

!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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM