简体   繁体   English

使用Powershell v2.0和XPATH读取包含多个属性的xml组件

[英]Using Powershell v2.0 nd XPATH to read xml component containing multiple attributes

I cannot find a suitable answer to this as all the examples relate to a regular or standard form of XML structure. 我找不到适合的答案,因为所有示例都与XML结构的常规或标准形式有关。 I am trying to search a directory of xml files, and I want to limit to those that have a particular "OrderDate" attribute specified in the xml. 我正在尝试搜索xml文件的目录,并且我想限制为那些在xml中指定了特定“ OrderDate”属性的文件。 So the PS script is to filter to a particular value, but I am unable to get PS to reference this attribute. 所以PS脚本要过滤到特定值,但是我无法让PS引用此属性。

<?xml version="1.0"?>
<Order Value="28.78" Lines="3" OrderDate="2016-03-21" SupplierName="ASupplier" OrderNo="1234">
<Line UnitCost="5.28" PackSize="Box 44" Quantity="1" ImpExpRef="1234" LineNo="1"/>
<Line UnitCost="18" PackSize="Box180" Quantity="1" ImpExpRef="1234" LineNo="2"/>
<Line UnitCost="5.5" PackSize="5Ltr" Quantity="1" ImpExpRef="1234" LineNo="3"/>
</Order>

As you can see, there are two nodes (or elements?): Order and Line, and there multiple attributes for each and don't have their own node, so I can't work out how to XPATH this file. 如您所见,有两个节点(或元素?):Order和Line,每个都有多个属性,并且没有自己的节点,因此我无法弄清楚如何对该文件进行XPATH

The current script just to isolate the attribute is: 当前用于隔离属性的脚本为:

[xml]$XmlDocument = Get-content "C:\example.xml"
$XPath = '//Order[@OrderDate]'    
$nodes = Select-Xml $XmlDocument -XPATH $xpath | Select-Object -ExpandProperty Node

and the results are: 结果是:

PS C:\Windows\system32> $nodes

OrderNo      : 1234
SupplierName : ASupplier
OrderDate    : 2016-03-21
Lines        : 3
Value        : 28.78
Line         : {Line, Line, Line}

It doesn't recognise @OrderDate as an attribute, or any other attribute for that matter, presumably because there are multiple attributes under "Order", it's as if it's ignoring it. 它不能将@OrderDate识别为属性或任何其他属性,大概是因为“ Order”下有多个属性,就好像忽略了它一样。 Same result if $XPath = '//Order[@*]' 如果$XPath = '//Order[@*]'

All examples I have researched assume one attribute per node (or element) 我研究的所有示例均假设每个节点(或元素)具有一个属性

"I tried what you suggested but it doesn't return any values as if it ignores it" “我尝试了您的建议,但它不会像忽略它一样返回任何值”

I maybe missing something about how you tried it or what you are trying to achieve actually (please clarify). 我可能会丢失一些有关您如何尝试或实际要达到的目标的信息(请澄清)。 The following is what I did exactly , and it returned the expected element : 以下是我的确切操作 ,它返回了预期的元素:

PS C:\Users\Foo> $XmlDocument = [xml]@"
>> <Order Value="28.78" Lines="3" OrderDate="2016-03-21" SupplierName="ASupplier" OrderNo="1234">
>> <Line UnitCost="5.28" PackSize="Box 44" Quantity="1" ImpExpRef="1234" LineNo="1"/>
>> <Line UnitCost="18" PackSize="Box180" Quantity="1" ImpExpRef="1234" LineNo="2"/>
>> <Line UnitCost="5.5" PackSize="5Ltr" Quantity="1" ImpExpRef="1234" LineNo="3"/>
>> </Order>
>> "@
>>
PS C:\Users\Foo> $XPath = "//Order[@OrderDate='2016-03-21']"
PS C:\Users\Foo> $nodes = Select-Xml $XmlDocument -XPATH $xpath | Select-Object -ExpandProperty Node
PS C:\Users\Foo> $nodes


Value        : 28.78
Lines        : 3
OrderDate    : 2016-03-21
SupplierName : ASupplier
OrderNo      : 1234
Line         : {Line, Line, Line}

UPDATE : 更新:

In response to your comment below, the XPath //Order[@OrderDate='2016-03-21'] returns Order element that have certain OrderDate attribute value. 为了响应您在下面的评论,XPath //Order[@OrderDate='2016-03-21']返回具有某些OrderDate属性值的Order元素 By Order element I mean the entire element leaving none of its part. 所谓Order元素 ,是指整个元素不遗余力。 You can't return Order element that contains only one attribute ie OrderDate attribute, because there is no such element in the original XML. 您不能返回仅包含一个属性(即OrderDate属性)的Order元素,因为原始XML中没有这样的元素。 You can, however, returns just the target attribute using the following XPath : 但是,您可以使用以下XPath仅返回target属性:

//Order/@OrderDate[.='2016-03-21']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM