简体   繁体   English

使用Powershell从名称空间中选择XML节点

[英]Select XML nodes from a namespace using Powershell

I have the following xml: 我有以下xml:

<?xml version="1.0" encoding="utf-8"?>
<userSettings>
  <setting name="TelephonyServerHost">
    <value>sipserver.domain.local</value>
  </setting>
  <setting name="SipServerFqdn">
    <value>sipserver.domain.local</value>
  </setting>
  <setting name="WebServicesHost">
    <value>websvc.domain.local</value>
  </setting>
  <setting name="KMSettings">
    <value>
      <KMIndexSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <AutoIndexEnabled>false</AutoIndexEnabled>
     </KMIndexSettings>
    </value>
  </setting>
</userSettings>

I am able to retrieve the values of the setting elements using xpath but I cannot figure out the correct syntax for querying the AutoIndexEnabled element using the namespace. 我可以使用xpath检索设置元素的值,但是我无法找出使用名称空间查询AutoIndexEnabled元素的正确语法。

This works as expected for reading the KMSettings or other nodes which do not have a namespace: 它可以按预期方式读取KMSettings或其他没有名称空间的节点:

$xml = New-Object -TypeName 'System.XML.XMLDocument'
$xml.Load($xmlFilePath)
$node = $xml.SelectSingleNode("//userSettings/setting[@name='KMSettings']")

But I can't figure out the syntax on how to query the AutoIndexEnabled element. 但是我不知道如何查询AutoIndexEnabled元素的语法。

Within PowerShell you can access XML nodes like properties, so this works: 在PowerShell中,您可以访问XML节点(如属性),因此可以正常工作:

($xml.DocumentElement.setting | ? name -eq 'KMSettings').value.KMIndexSettings.AutoIndexEnabled

And here is a working XPATH solution: 这是一个有效的XPATH解决方案:

[string]$xpath="//userSettings/setting[@name='KMSettings']/value/KMIndexSettings/AutoIndexEnabled"       
$xml.SelectSingleNode($xpath)

I don't understand the problem. 我不明白这个问题。 The namespaces doesn't matter here because your xml-sample doesn't contain prefixed elements or a default namespace. 命名空间在这里无关紧要,因为您的xml-sample不包含前缀元素或默认命名空间。 You can access the element like this: 您可以像这样访问元素:

$xml.SelectNodes("//AutoIndexEnabled")

or 要么

$xml.SelectNodes("//setting[@name='KMSettings']//AutoIndexEnabled")

Output: 输出:

#text
-----
false

PS> $xml.SelectNodes("//AutoIndexEnabled").InnerText
false

I this particular example, how would you select the value of the attribute with name "xmlns:xsi"? 在这个特定示例中,如何选择名称为“ xmlns:xsi”的属性的值?

<value>
  <KMIndexSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ... >
    <AutoIndexEnabled>false</AutoIndexEnabled>
 </KMIndexSettings>
</value>

I would expect to see the output: " http://www.w3.org/2001/XMLSchema-instance " 我希望看到输出:“ http://www.w3.org/2001/XMLSchema-instance

This is what I'm trying.The colon is throwing off my script. 这就是我正在尝试的。冒号正在删除我的脚本。 I get the following error: "Unexpected token ':id' in expression or statement." 我收到以下错误:“表达式或语句中出现意外的标记':id'”。

([xml](gc $files[$i])).contentHaul.constant.typedValue.value.a:id

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

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