我不是 PowerShell 或 XPath 方面的专家,但我在如何解决这个看似简单的问题上遇到了一些困难。假设我有这个 XML 文档:
<?xml version="1.0" encoding="utf-8"?>
<Cars>
<Car>
<Name>Car1</Name>
<Colors>
<Color>
<Name>Indian yellow</Name>
<Effects>
<Effect>Blur</Effect>
<Effect>Shadow</Effect>
</Effects>
</Color>
<Color>
<Name>Fireapple red</Name>
<Effects>
<Effect>Shadow</Effect>
</Effects>
</Color>
</Colors>
</Car>
<Car>
<Name>Car2</Name>
<Colors>
<Color>
<Name>Indian yellow</Name>
<Effects>
<Effect>Blur</Effect>
<Effect>Shadow</Effect>
<Effect>Saturated</Effect>
</Effects>
</Color>
<Color>
<Name>Chrome black</Name>
<Effects>
<Effect>Saturated</Effect>
</Effects>
</Color>
</Colors>
</Car>
<Car>
<Name>Car3</Name>
<Colors>
<Color>
<Name>Indian yellow</Name>
<Effects>
<Effect>Shadow</Effect>
<Effect>Saturated</Effect>
</Effects>
</Color>
<Color>
<Name>Fireapple red</Name>
<Effects>
<Effect>Saturated</Effect>
</Effects>
</Color>
</Colors>
</Car>
</Cars>
如何使用 Select-Xml 来选择例如颜色效果为“饱和”的汽车名称?请注意,我需要独特的汽车收藏,例如即使两种颜色都具有“饱和”效果,Car2 也不得选择两次。
最佳答案
XPath /Cars/Car[Colors/Color/Effects/Effect = 'Saturated']/Name
应该可以。
关于xml - 如何根据子项选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360367/