【问题标题】:Getting the element name of an attribute in xml using powershell使用powershell获取xml中属性的元素名称
【发布时间】:2017-03-17 07:23:58
【问题描述】:

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

假设我想获取属性“toy”的元素名称“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}

【问题讨论】:

    标签: xml powershell xml-parsing


    【解决方案1】:

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

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

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

    $toy.get_Name()
    

    【讨论】:

      【解决方案2】:

      这是 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()
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-10
            • 2012-01-21
            • 2015-05-20
            相关资源
            最近更新 更多