【发布时间】: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