列出子节点元包含某个值的所有节点 123456 [英] List all nodes which sub-node meta contains some value 123456

查看:24
本文介绍了列出子节点元包含某个值的所有节点 123456的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它们的子节点元包含 123456,我希望下面的脚本输出 Pdt 1Pdt3,但它不输出任何内容.什么是正确的语法?

I want my script below to output Pdt 1 and Pdt3 if their sub-node meta contains 123456, but it outputs none. What's the right syntax?

我知道问题来自于 @id='$_.id' 中的错误标准语法

I know problem comes from bad criteria syntax @id='$_.id' in

ForEach-Object {
    $meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$_.id']/metas/meta[@code='$meta_code']")
}

这是整个脚本的一部分:

which is part of whole script:

$xml=[xml]@'
<?xml version="1.0" encoding="iso-8859-1"?>
<catalogue>
  <produits>
    <produit id="pdt1" libelle="produit 1" cat="PDT">
      <metas date="2015.07.24">
        <meta code="123456" value="123456"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
    <produit id="pdt2" libelle="produit 2" cat="PDT">
      <metas date="2015.07.24">
        <meta code="123456" value="abcdef"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
    <produit id="pdt3" libelle="produit 3" cat="PDT">
      <metas date="2015.07.24">
        <meta code="123456" value="123456"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
  </produits>
</catalogue>
'@

$meta_code = "123456"

$xml.catalogue.produits.produit | where {$_.cat.startsWith("PDT")} | ForEach-Object {
    $meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$_.id']/metas/meta[@code='$meta_code']")
    if($meta) {Write-Host $_.libelle "in"  $meta.code}
}

推荐答案

我认为问题在于您的 XQuery 字符串:

I think the problem is with your XQuery string:

$meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$_.id']/metas/meta[@code='$meta_code']")

PowerShell 将 $_.id 解释为 $_.ToString() + ".id",在您的情况下,最终是 pdt1.id.当然,没有节点与该 ID 匹配.

PowerShell interprets $_.id as $_.ToString() + ".id" which, in your case, ends up being pdt1.id. Of course, no nodes match that id.

相反,将 $_.id 包装成一个表达式,$($_.id):

Instead, wrap $_.id as an expression, $($_.id):

$meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$($_.id)']/metas/meta[@code='$meta_code']")

或者,更好的是,使用 -f 字符串格式运算符:

Or, better yet, use the -f string format operator:

$xquery = "//catalogue/produits/produit[@id='{0}']/metas/meta[@code='{1}']" -f $_.id,$meta_code
$meta = $xml.SelectSingleNode($xQuery)

您可以完全放弃对 SelectSingleNode 的调用,因为您已经在 ForEach-Object 脚本块中获得了该节点.你可以找到你想要的元节点:

You could forgo the call to SelectSingleNode completely, because you've already got the node in your ForEach-Object script block. You can find the meta node you want with:

$xml.catalogue.produits.produit |
    Where-Object { $_.cat -like 'PDT*' } |
    ForEach-Object { 
        $produit = $_
        $meta = $produit.metas.meta | Where-Object { $_.code -eq $meta_code }
        if( $meta )
        {
            Write-Host -Message ('{0} in {1}' -f $produit.libelle,$meta.code)
        }
    }

这篇关于列出子节点元包含某个值的所有节点 123456的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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