我希望你可以协助我理解PowerShell中的XML。我有几个像这样的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.example.com/xml/catalog/2006-10-31">
<product product-id="11210">
...
<available-flag>true</available-flag>
<online-flag>false</online-flag>
<online-flag site-id="ru">true</online-flag>
<online-flag site-id="fr">true</online-flag>
<online-flag site-id="uk">false</online-flag>
<online-flag site-id="de">true</online-flag>
...
</product>
<product product-id="50610">
...
<available-flag>true</available-flag>
<online-flag>true</online-flag>
<online-flag site-id="ru">false</online-flag>
<online-flag site-id="fr">true</online-flag>
<online-flag site-id="uk">false</online-flag>
<online-flag site-id="de">fasle</online-flag>
...
</product>
<product product-id="82929">
...
<available-flag>true</available-flag>
<online-flag>true</online-flag>
<online-flag site-id="ru">false</online-flag>
<online-flag site-id="fr">true</online-flag>
<online-flag site-id="uk">false</online-flag>
<online-flag site-id="de">true</online-flag>
...
</product>
</catalog>
我需要在PowerShell中获取两个元素的值:
<online-flag>
(不带site-id
属性)<online-flag site-id="ru">
关于 product-id="50610"
的产品。
$Path = "C:\Temp\0\2017-08-12_190211.xml"
$XPath = "/ns:catalog/ns:product[@product-id='50610']"
$files = Get-ChildItem $Path | Where {-not $_.PSIsContainer}
if ($files -eq $null) {
return
}
foreach ($file in $files) {
[xml]$xml = Get-Content $file
$namespace = $xml.DocumentElement.NamespaceURI
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("ns", $namespace)
$product = $xml.SelectSingleNode($XPath, $ns)
}
有几个问题:
With this code I am able to select the needed product node. PowerShell shows:
online-flag : {true, online-flag, online-flag, online-flag...}
But how then I can select the values of the needed
online-flag
elements (if it is possible both ways: XPath one and the object one)?Is it possible to select a node in the "object" way? Like this:
$product = $xml.catalog.product | Where-Object {$_."product-id".value -eq "50610"}
If I have several files, what is the best way to select filename, global online-flag (without attributes), specific online-flag?
<xml><ns:node>val</ns:node>
,但我在xmlfile
中找不到它。 - Timo