【问题标题】:Select-Xml shows only the child nodes that are in the first nodeSelect-Xml 仅显示第一个节点中的子节点
【发布时间】:2014-02-11 23:12:34
【问题描述】:

当且仅当它在第一个父元素中时,以下将显示一个子元素。

$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

输出

commonchild otherchild        
----------- ----------        
Hi there.   This is displayed.
Hi again.   So is this.       
Well hello.                   
Cheers.        

我们怎样才能显示所有父母的所有子元素?例如,以下工作,但有时我们不知道所有的子元素名称。

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.   

【问题讨论】:

  • 星号是“任何” - 所以可能是//* ?
  • @AlexeiLevenkov 那不行。
  • 它不做什么? Select-Xml -Content $xml -XPath "//*" 为您提供所有节点...您只需要弄清楚如何处理它们,因为“所有”包括 root 作为第一个节点,我认为这会混淆输出...尝试使用您的原始代码 /root//*非空输出
  • @AlexeiLevenkov 我尝试了您在我的原始代码中使用/root//* 的建议。输出的只有鞋子 commonchild 和 other child;它没有显示失踪的孩子。换句话说,我运行这个Select-Xml -Content $xml -XPath "/root//*" | select -ExpandProperty node 没有任何进一步的成功。

标签: xml powershell xpath powershell-3.0


【解决方案1】:
$pars = (Select-Xml -XPath "//par" -Content $xml)
$childNodes = ($pars.Node.ChildNodes.Name | select -Unique)
$pars.节点 |选择 $childNodes

普通孩子 其他孩子 失踪孩子
------------ ---------- ------------
你好呀。这是显示的。
你好,我们又见面了。这也是。
你好。这是缺失的。
干杯。这个也是。

所以XmlNode.ChildNodes 是一个隐藏属性,如果你在它上面抛出-Force 参数,它只会被Get-Member 暴露。基本上,我使用该属性来获取子节点的名称,然后对其进行过滤,因此我只有一个使用select -Unique 命令。然后我将这些名称保存在一个名为$childNodes 的变量中,并将该变量用作最终select-Property 参数的值。

【讨论】:

  • 这正是我想要的。谢谢你。我想知道它将如何处理非常大的文件(例如 50 MB XML 文件)。 IE。在运行 select $childnodes 之前,它可能必须先读取整个文件。
【解决方案2】:

尝试使用Format-Table (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 &lt;name&gt; 旨在扩展作为集合的属性 - 将集合的项目展平到管道中。虽然它也可以显示特定标量属性的值(我想是一种副作用),但正如您所看到的,它并不总是能很好地工作。 :-)

【讨论】:

  • 这确实可以挑选出我想要显示的属性。然后,我可以通过select property1, property2, property3 使用特定的投影。 @Nacimota 的回答更符合我的要求,因为它消除了对特定投影的需要。
猜你喜欢
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多