选择Xml仅显示第一个节点中的子节点
以下内容将显示子元素,当且仅当它位于第一个父元素中时选择Xml仅显示第一个节点中的子节点,xml,powershell,xpath,powershell-3.0,Xml,Powershell,Xpath,Powershell 3.0,以下内容将显示子元素,当且仅当它位于第一个父元素中时 $xml = @" <?xml version="1.0" encoding="utf-8"?> <root> <par><commonchild>Hi there.</commonchild><otherchild>This is displayed.</otherchild></par> <par><commo
$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<root>
<par><commonchild>Hi there.</commonchild><otherchild>This is displayed.</otherchild></par>
<par><commonchild>Hi again.</commonchild><otherchild>So is this.</otherchild></par>
<par><commonchild>Well hello.</commonchild><missingchild>This is missing.</missingchild></par>
<par><commonchild>Cheers.</commonchild><missingchild>So is this.</missingchild></par>
</root>
"@
cls
Select-Xml -Content $xml -XPath "//par" | select -ExpandProperty node
我们如何显示所有父元素的所有子元素?例如,下面的方法可以工作,但有时我们不知道所有的子元素名称
cls
Select-Xml -Content $xml -XPath "//par" |
select -ExpandProperty node |
select commonchild, otherchild, missingchild
输出
commonchild otherchild missingchild
----------- ---------- ------------
Hi there. This is displayed.
Hi again. So is this.
Well hello. This is missing.
Cheers. So is this.
尝试使用
格式表(ft)选择要显示的属性:
C:\PS> Select-Xml -Xml $xml -XPath '/root//*' | % Node | ft name,'#text'
Name #text
---- -----
par
commonchild Hi there.
otherchild This is displayed.
par
commonchild Hi again.
otherchild So is this.
par
commonchild Well hello.
missingchild This is missing.
par
commonchild Cheers.
missingchild So is this.
Select-ExpandProperty
用于扩展属于集合的属性-将集合的项展平到管道中。虽然它也可以显示特定标量属性的值(我想这是一个副作用),但正如您所看到的,它并不总是工作得很好。:-) $pars=(选择Xml-XPath”//par“-Content$Xml)
$childNodes=($pars.Node.childNodes.Name |选择-唯一)
$pars.Node |选择$childNodes
commonchild其他Child缺少Child
----------- ---------- ------------
你好。这将显示。
你好。这也是。
你好。这个不见了。
干杯这也是。
因此,只有在抛出-Force
参数时,Get成员才能公开隐藏属性。基本上,我使用该属性获取子节点的名称,然后使用select-Unique
命令对其进行过滤,这样每个节点就只有一个。然后我将这些名称保存在一个名为$childNodes
的变量中,并将该变量用作最后一个select
的-Property
参数的值 星号是“任何”-所以很可能/*
?@AlexeiLevenkov不这样做。它不做什么<代码>选择Xml-Content$Xml-XPath“/*”
提供所有节点。。。您只需要弄清楚如何处理它们,因为“all”包括根节点
作为第一个节点,我认为这会混淆输出。。。使用原始代码尝试/root/*
,以获得一些非空代码output@AlexeiLevenkov我尝试了您的建议,将/root/*
与我的原始代码一起使用。仅输出子级和其他子级;它没有显示失踪的孩子。换句话说,我运行了这个Select Xml-Content$Xml-XPath”/root/*“| Select-ExpandProperty节点
,但没有取得任何进一步的成功。这确实可以选择我想要显示的属性。然后,我可以通过选择property1、property2、property3
使用特定的投影@Nacimota的答案更符合我的要求,因为它不需要特定的投影。这正是我想要的。非常感谢。我想知道它将如何处理非常大的文件(例如50MB的XML文件)。也就是说,在运行select$childnodes
之前,它可能必须先读取整个文件。
C:\PS> Select-Xml -Xml $xml -XPath '/root//*' | % Node | ft name,'#text'
Name #text
---- -----
par
commonchild Hi there.
otherchild This is displayed.
par
commonchild Hi again.
otherchild So is this.
par
commonchild Well hello.
missingchild This is missing.
par
commonchild Cheers.
missingchild So is this.
$pars = (Select-Xml -XPath "//par" -Content $xml)
$childNodes = ($pars.Node.ChildNodes.Name | select -Unique)
$pars.Node | select $childNodes
commonchild otherchild missingchild
----------- ---------- ------------
Hi there. This is displayed.
Hi again. So is this.
Well hello. This is missing.
Cheers. So is this.