Powershell根据子节点值选择父xml节点并添加子元素

Posted

技术标签:

【中文标题】Powershell根据子节点值选择父xml节点并添加子元素【英文标题】:Powershell select parent xml node based on child node value and add child element 【发布时间】:2021-12-28 12:26:36 【问题描述】:

查看下面的xml

    <catalog>
        <books>
           <book id="bk101">
              <author>Gambardella, Matthew</author>
              <title>XML Developer's Guide</title>
              <genre>Computer</genre>
              <price>44.95</price>
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim</author>
              <title>Midnight Rain</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
           <book id="bk103">
              <author>Corets, Eva</author>
              <title>Maeve Ascendant</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-11-17</publish_date>
              <description>After the collapse of a nanotechnology 
              society in England, the young survivors lay the 
              foundation for a new society.</description>
           </book>
       </books>
    </catalog>

我想在powershell中选择作者是Corets,Eva的书节点/元素,然后添加书的子元素,另一个元素(价格的兄弟,直接在价格之后),例如长度。以及长度的新值“456”

因此,我运行代码后 book 元素的(部分)xml 代码如下所示:

<catalog>
    <books>
       <book id="bk103">
          <author>Corets, Eva</author>
          <title>Maeve Ascendant</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <length>456</length>        
          <publish_date>2000-11-17</publish_date>
          <description>After the collapse of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society.</description>
       </book>
       <book>
        ....
       </book>
    </books>
<etc>

有人可以帮忙吗?现在卡了两天。谢谢

【问题讨论】:

请添加您迄今为止尝试过的代码,包括任何(不正确的)输出。 【参考方案1】:

您可以使用XMLNode.InsertAfter 方法。

例子:

[xml]$books = Get-Content .\books.xml
$corets = $books.catalog.books.book | Where-Object  $_.author -eq 'Corets, Eva' 
$coretsPrice = $corets.SelectSingleNode('price')
$coretsLength = $books.CreateElement('length')
$coretsLength.InnerText = 495
$corets.InsertAfter($coretsLength, $coretsPrice)

【讨论】:

我昨天在这里发表了感谢评论,但由于某种原因它消失了。再次感谢,非常感谢!【参考方案2】:

您可以使用以下 XPath 表达式仅选择子节点 &lt;author&gt; 的文本值为 Corets, Eva&lt;book /&gt; 节点:

//book[author[. = 'Corets, Eva']]

所以你可以这样做:

# Create a new XML document
$newDocument = [xml]'<catalog><books></books></catalog>'
$newBooks = $newDocument.SelectSingleNode('/catalog/books')

# Loop through all relevant <book> nodes in the existing document
foreach($book in $existingDocument.SelectNodes('//book[author[. = 'Corets, Eva']]'))
    # import book node to new document
    $newBook = $newDocument.ImportNode($book, $true)

    # add new required child node
    $newLength = $newDocument.CreateElement('length')
    $newLength.InnerText = '456'

    # insert new node after `price`
    [void]$newBook.InsertAfter($newLength, $newBook.SelectSingleNode('./price'))
    # insert new book node under /catalog/books
    [void]$newBooks.AppendChild($newBook)

【讨论】:

非常感谢 Mathias,使用了 dugas 解决方案,因为它首先到达这里并有效。但你的看起来也很棒。

以上是关于Powershell根据子节点值选择父xml节点并添加子元素的主要内容,如果未能解决你的问题,请参考以下文章

如何使用xsl根据属性将xml的兄弟节点更改为父节点和子节点

Delphi TreeView 选择父节点,选择所有子节点

C#中怎样修改XML中子节点的值

Powershell XML选择具有特定值的节点

将属性和值填充到第4个父节点的XML文件的所有父节点

根据XPath中子节点的double值查找父节点