gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:23:15 25 4
gpt4 key购买 nike

我想使用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>

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

到目前为止,这就是我所拥有的。
[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}

最佳答案

给定一个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 :

关于xml - 使用Powershell在xml中获取属性的元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com