繁体   English   中英

使用Powershell在xml中获取属性的元素名称

[英]Getting the element name of an attribute in xml using powershell

我想使用powershell获取属性的节点名称。 任何人都可以让我知道我们是否具有内置功能。

以下是我的名为Pricefile.xml的xml文件

 <model type="model1" name="default" price="12.12" date="some_value">
  <PriceData>
    <item name="watch" price="24.28" date="2013-12-01"/>
    <item name="toy" price="22.34" date="2013-12-02"/>
    <item name="bread" price="24.12" date="2013-12-03"/>
  </PriceData>
 </model>

假设我想获取属性“玩具”的元素名称“项目”。 我如何获得这些数据?

到目前为止,这就是我所拥有的。

[xml]$item = get-content pricefile.xml
$item.SelectNodes("//item/@*")

这给了我下面的输出,但是我不知道如何从这里或它的父节点获取属性的元素。

#text                                                                                                                                                  
-----                                                                                                                                                  
watch                                                                                                                                                  
24.28                                                                                                                                                  
2013-12-01                                                                                                                                             
toy                                                                                                                                                    
22.34                                                                                                                                                  
2013-12-02                                                                                                                                             
bread                                                                                                                                                  
24.12                                                                                                                                                  
2013-12-03 

如果我使用以下任何命令,都不会输出。

[xml]$item = get-content pricefile.xml

$item.SelectNodes("//item/@*").parentnode

$item.SelectNodes("//item/@*") | where {$_.parentnode}

首先,选择name属性值为toy的元素:

$toy = $item.SelectSingleNode("//*[@name='toy']")

由于PowerShell试图提供帮助,并将name属性作为对象的属性公开,因此我们需要对实际的标签Name属性使用属性访问器:

$toy.get_Name()

这是xpath的方式:

[xml]$item = @'
  <model type="model1" name="default" price="12.12" date="some_value">
    <PriceData>
      <item name="watch" price="24.28" date="2013-12-01"/>
      <item name="toy" price="22.34" date="2013-12-02"/>
      <item name="bread" price="24.12" date="2013-12-03"/>
    </PriceData>
  </model>
'@

(Select-XML -Xml $item -XPath "//item[@name=""toy""]").Node
(Select-XML -Xml $item -XPath "//item[@name=""toy""]").Node.get_Name()

给定一个XmlAttribute对象开始,您可以通过OwnerElement属性获得父元素。 请注意, ParentNode在这里将不起作用,因为XmlAttribute上的值始终为空:

λ $myXmlAttr = $item.SelectSingleNode("//item/@*")
λ $myXmlAttr.OwnerElement.LocalName
item
λ $myXmlAttr | Format-List -Property *


#text           : watch
ParentNode      :
Name            : name
LocalName       : name
NamespaceURI    :
Prefix          :
NodeType        : Attribute
OwnerDocument   : #document
Value           : watch
SchemaInfo      : System.Xml.XmlName
InnerText       :
Specified       : True
OwnerElement    : item
InnerXml        :
BaseURI         :
ChildNodes      : {#text}
PreviousSibling :
NextSibling     :
Attributes      :
FirstChild      : #text
LastChild       : #text
HasChildNodes   : True
IsReadOnly      : False
OuterXml        : name="watch"
PreviousText    :

这是获取所需信息的另一种方法:

$item.model.PriceData.Item | ? name -eq toy

暂无
暂无

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

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