使用powershell获取xml中属性的元素名称 [英] Getting the element name of an attribute in xml using powershell

查看:26
本文介绍了使用powershell获取xml中属性的元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 powershell 获取属性的节点名称.任何人都可以让我知道我们是否有相同的内置功能.<​​/p>

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

 <价格数据><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"/></价格数据></模型>

假设我想获取属性toy"的元素名称item".我如何获得这些数据?

这是我目前所拥有的.

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

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

#text-----手表24.282013-12-01玩具22.342013-12-02面包24.122013-12-03

如果我使用以下任何命令,则不会有任何输出.

[xml]$item = get-content pricefile.xml$item.SelectNodes("//item/@*").parentnode$item.SelectNodes("//item/@*") |其中 {$_.parentnode}

解决方案

给定一个 XmlAttribute 对象开始,你可以通过 OwnerElement 属性.请注意, ParentNode 在这里不起作用,因为它的值在 XmlAttribute 上始终为空:

λ $myXmlAttr = $item.SelectSingleNode("//item//@*")λ $myXmlAttr.OwnerElement.LocalName物品λ $myXmlAttr |格式列表-属性 *#text : 看父节点:姓名:姓名本地名称:名称命名空间URI :字首          :节点类型:属性所有者文档:#document价值:手表SchemaInfo : System.Xml.XmlName内文:指定:真所有者元素:项目内部Xml :基本URI :子节点:{#text}上一个兄弟姐妹:下一个兄弟姐妹:属性      :第一个孩子:#text最后一个孩子:#textHasChildNodes : 真IsReadOnly : 假OuterXml : name="watch"上一篇:

I would like to get the node name of an attribute using powershell. Can anyone please let me know if we have a built in function for the same.

Following is my xml file called pricefile.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>

Say I want to get the element name "item" for the attribute "toy". How can I get this data?

This is what I have so far.

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

which gives me the below output, but I do not know how to get the element of the attribute from here or it's parent node.

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

If I use any of the below commands I get no output.

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

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

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

解决方案

Given an XmlAttribute object to start with, you can get parent element via OwnerElement property. Notice that ParentNode won't work here since its value is always empty on 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    :

这篇关于使用powershell获取xml中属性的元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆