Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Powershell中XPath包含变量的Xml文档中选择单个节点?_Xml_Powershell_Xpath - Fatal编程技术网

如何从Powershell中XPath包含变量的Xml文档中选择单个节点?

如何从Powershell中XPath包含变量的Xml文档中选择单个节点?,xml,powershell,xpath,Xml,Powershell,Xpath,在Powershell中操作XML文件时,我试图检测节点是否已经存在 不幸的是,我正在处理的XmlDocument使用了默认名称空间,我认为这会使事情变得混乱 下面是XML的一个子集 <?xml version="1.0" encoding="utf-8"?> <Configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration" xmlns:ext="http://www.sdltridion.com/2

在Powershell中操作XML文件时,我试图检测节点是否已经存在

不幸的是,我正在处理的XmlDocument使用了默认名称空间,我认为这会使事情变得混乱

下面是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>
我想检查Powershell中是否存在name属性设置为MyExtension的编辑器节点,如果不存在,则添加它

除了一些名称空间的诡计之外,添加它很好,但检测到它让我感到困惑

以下是我迄今为止所尝试的:

# 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"
}

我一直在兜圈子,尝试各种不同的方法。有人能帮我吗?

看来这应该行得通:

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

繁荣谢谢但愿我能找回生命的最后两个小时。。您知道在Powershell中以这种方式处理xml的好资源吗?在Powershell中转换为[xml]后,您可以使用点表示法来导航节点,就像您导航PS对象的属性一样。不完整的SampleEditor会导致节点丢失,我会收到namespacemanager错误,我现在没有时间弄清楚。但是您的xpath看起来是错误的。只有第一个节点配置定义了命名空间。此外,name是一个属性,使其成为[@name=MyExtension]