簡體   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