XML 使用 SelectNodes 获取子节点为空值的节点

XML use SelectNodes to get nodes that have a subnode with a null value

目前我们得到一个节点列表,其中特定节点的值为 0.00、0 或 0,00。

hoofddocument.SelectNodes("//Product[./polispremie_1[.='0,00' or .='0' or .='0.00' ]]")

但如果 'polispremie_1' 节点为空,我们还希望包括 Product 节点。

我希望在当前的 xpath 中添加一些东西。

我试过添加“或 .= null”,但这不起作用。

xml示例:

<Products>
    <Product>
        <polispremie_1/>
    </Product>
    <Product>
        <polispremie_1>0</polispremie_1>
    </Product>
</products>

在此示例中,第一个产品的 polispremie_1 为空值。 我想通过上述查询获得两者,但我只获得了第二个产品。

总是很难说出人们认为 XML 中的 null 是什么,但是 //Product[not(polispremie_1) or polispremie_1[.='0,00' or .='0' or .='0.00' ]] 会 select Product 没有 [=13] 的元素=] child 以及您的价值比较条件成立的那些。

一个VB例子

    Dim testXE As XElement = <Products>
                                 <Product>
                                     <polispremie_1/>
                                 </Product>
                                 <Product>
                                     <polispremie_1>0</polispremie_1>
                                 </Product>
                                 <Product>
                                     <polispremie_1>0.0</polispremie_1>
                                 </Product>
                             </Products>

    Dim ie As IEnumerable(Of XElement)
    Dim d As Decimal
    ie = From el As XElement In testXE...<polispremie_1>
           Where (Decimal.TryParse(el.Value, Globalization.NumberStyles.Any,
                                  Globalization.CultureInfo.InvariantCulture,
                                  d) AndAlso d = 0) OrElse el.Value = ""
           Select el

    'look at results in ie