使用元素名称中的命名空间更新 Xml 文件 (PowerShell) [英] Update Xml file with namespace in element names (PowerShell)

查看:22
本文介绍了使用元素名称中的命名空间更新 Xml 文件 (PowerShell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml 文件,我想根据一个变量的值更新它.

I have a xml file which I want to update based on a value from a variable.

我想要这样的东西

$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 属性.

That is, I am trying to extract a specific XML element of interest and to update its ObjectName attribute.

为什么这不起作用?

另一种选择是使用点表示法,这是我更喜欢的,但我不知道如何过滤此点之后的属性:

Another option is to use dot notation, which is what I prefer, but I do not know how to filter the attribute after this point:

$xml2.Executable.Variables.Variable

我必须使用过滤器 @dts:ObjectName = 'BATCH_JOB_ID' 我可以这样做:

I have to use the filter @dts:ObjectName = 'BATCH_JOB_ID' I can do like:

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

但我想写 ObjectName = 'BATCH_JOB_ID 而不是 [0].那可能吗?我该怎么写:

But instead of [0] I would like to write ObjectName = 'BATCH_JOB_ID. Is that possible? How can I write something like:

$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] 实例:


The alternative is to let Select-Xml parse the XML file into a DOM for you, without the need to create an [xml] instance manually first:

$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")

这篇关于使用元素名称中的命名空间更新 Xml 文件 (PowerShell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆