简体   繁体   English

如何从Powershell中的Xml Document中选择单个节点,其中XPath包含变量?

[英]How to select a single node from an Xml Document in Powershell where the XPath contains a variable?

I'm hitting a brick wall trying to detect whether a node already exists while manipulating an XML file in Powershell. 在Powershell中操作XML文件时,我正试图检测一个节点是否已经存在。

Unfortunately the XmlDocument I'm processing uses default namespaces which I think confuses things. 不幸的是,我正在处理的XmlDocument使用默认命名空间,我认为这会混淆事情。

Here's a subset of the XML 这是XML的一个子集

<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration" xmlns:ext="http://www.sdltridion.com/2009/GUI/extensions" xmlns:cmenu="http://www.sdltridion.com/2009/GUI/extensions/ContextMenu">
    <editor name="MyExtension">
      <installpath xmlns="http://www.sdltridion.com/2009/GUI/Configuration">C:\Program Files (x86)\Tridion\web\WebUI\Editors\MyExtension</installpath>
      <configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration">Editor\Configuration\editor.config</configuration>
      <vdir xmlns="http://www.sdltridion.com/2009/GUI/Configuration">MyExtension</vdir>
    </editor>
  </editors>
</Configuration>

I want to check whether the editor node with the name attribute set to "MyExtension" exists in Powershell and add it if it doesn't. 我想检查名称属性设置为“MyExtension”的编辑器节点是否存在于Powershell中,如果不存在则添加它。

Adding it is fine (apart from some namespace shenanigans) but detecting it has me stumped. 添加它很好(除了一些命名空间恶作剧)但检测它让我难过。

Here's what I've tried so far: 这是我到目前为止所尝试的:

# Update System.config
$filename = $tridionInstallLocation + '\web\WebUI\WebRoot\Configuration\System.config'
$conf = [xml](gc $filename)

[System.Xml.XmlNamespaceManager] $nsm = new-object System.Xml.XmlNamespaceManager $conf.NameTable
$nsm.AddNamespace("x", "http://www.sdltridion.com/2009/GUI/Configuration")

# Editor
$xpath = "//x:Configuration/x:editors/x:editor[name=MyExtension]"
# $existingModelNode = $conf.SelectSingleNode($xpath, $nsm)
$existingModelNode = Select-Xml $conf -XPath $xpath
Write-Host $existingEditorNode # This is blank always
if($existingEditorNode -eq $null)
{
    $editors = [System.Xml.XmlElement]$conf.Configuration.editors
    $myElement = $conf.CreateElement("editor", $nsm.LookupNamespace("x"))
    $nameAttr = $myElement.SetAttribute("name", $name)
    $myElement.InnerXml = "<installpath xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $editorInstallLocation + "</installpath><configuration xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $editorConfigFile + "</configuration><vdir xmlns='http://www.sdltridion.com/2009/GUI/Configuration'>" + $name + "</vdir>"
    $editors.AppendChild($myElement)
}
else
{
    Write-Host "Editor node already exists in System.config with name $name, skipping"
}

I've been going round in circles trying various different methods. 我一直在尝试各种不同的方法。 Can anyone help me out? 谁能帮我吗?

看起来这应该工作:

@($config.Configuration.editors.editor.name) -contains 'MyExtension'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM