簡體   English   中英

使用元素名稱中的命名空間更新 Xml 文件 (PowerShell)

[英]Update Xml file with namespace in element names (PowerShell)

我有一個 xml 文件,我想根據變量的值更新它。

我想做這樣的事情

$ns = @{ dts = 'www.microsoft.com/SqlServer/Dts' }
$xml2 =  [XML](Get-Content "C:\Users\David\Documents\New.dtsx")

$xml2 | 
Select-Xml -XPath "/dts:Executable/dts:Variables/dts:Variable[@dts:ObjectName = 'BATCH_JOB_ID']/dts:VariableValue"  -Namespace $ns | 
Select-Object -ExpandProperty Node |
Select-Object -ExpandProperty '#text' = "$BATCH_JOB_ID"

也就是說,我正在嘗試提取感興趣的特定 XML 元素並更新其ObjectName屬性。

為什么這不起作用?

另一種選擇是使用點符號,這是我更喜歡的,但我不知道如何在這一點之后過濾屬性:

$xml2.Executable.Variables.Variable

我必須使用過濾器@dts:ObjectName = 'BATCH_JOB_ID'我可以這樣做:

$xml2.Executable.Variables.Variable.VariableValue[0].'#text'

但是我想寫ObjectName = 'BATCH_JOB_ID而不是 [0] 。 那可能嗎? 我怎么能寫這樣的東西:

$xml2.Executable.Variables.Variable.VariableValue[BATCH_JOB_ID].'#text'
$ns = @{ dts = 'www.microsoft.com/SqlServer/Dts' }
$xml2 = [xml](Get-Content -Raw C:\Users\David\Documents\New.dtsx)
$xpathQuery = "/dts:Executable/dts:Variables/dts:Variable[@dts:ObjectName = 'BATCH_JOB_ID']"

# Use XPath to target the element of interest, as a
# [Microsoft.PowerShell.Commands.SelectXmlInfo] instance whose .Node property is
# the targeted [System.Xml.XmlElement] instance.
# Call .SetAttribute() to update the 'ObjectName' attribute in-place.
(Select-Xml -Xml $xml2 -XPath $xpathQuery -Namespace $ns).
  Node.SetAttribute('ObjectName', $NEW_BATCH_JOB_ID)
}

# Save back to original file.
$xml2.Save("C:\Users\David\Documents\New.dtsx")

但是,由於您首先要自己解析 XML,因此沒有理由參與
Select-Xml

$xml2 = [xml](Get-Content -Raw C:\Users\David\Documents\New.dtsx)
$xpathQuery = "/dts:Executable/dts:Variables/dts:Variable[@dts:ObjectName = 'BATCH_JOB_ID']"

# Create the namespace-manager instance.
$nsm = [System.Xml.XmlNamespaceManager]::new($xml2.NameTable)
$nsm.AddNameSpace('dts', 'www.microsoft.com/SqlServer/Dts')

# Locate the element of interest...
$targetEl = $xml2.SelectSingleNode($xpathQuery, $nsm)
# ... and update its attribute.
$targetEl.SetAttribute('ObjectName', $NEW_BATCH_JOB_ID)

# Save back to original file.
$xml2.Save("C:\Users\David\Documents\New.dtsx")

另一種方法是讓Select-Xml為您將 XML 文件解析為 DOM,而無需先手動創建[xml]實例:

$ns = @{ dts = 'www.microsoft.com/SqlServer/Dts' }
$xpathQuery = "/dts:Executable/dts:Variables/dts:Variable[@dts:ObjectName = 'BATCH_JOB_ID']"

# Use XPath to target the element of interest, as a
# [Microsoft.PowerShell.Commands.SelectXmlInfo] instance whose .Node property is
# the targeted [System.Xml.XmlElement] instance.
$targetEl = (Select-Xml -LiteralPath C:\Users\David\Documents\New.dtsx -XPath $xpathQuery -Namespace $ns).Node

# Call .SetAttribute() to update the 'ObjectName' attribute in-place.
$targetEl.SetAttribute('ObjectName', $NEW_BATCH_JOB_ID)

# Save back to original file; the .OwnerDocument property provides
# access to the enclosing document.
$targetEl.OwnerDocument.Save("C:\Users\David\Documents\New.dtsx")

暫無
暫無

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

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