Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Or in XPath expression to select from multiple node names

Given XML like this...

<Rule scope="node">
    <Property>regKeyExists</Property>
    <Path>HKLM\SOFTWARE\MozillaPlugins\ K:@microsoft.com/GENUINE</Path>                             
    <Operator>-eq</Operator>
    <Value>true</Value>
</Rule>
<Or scope="node">
    <Rule>
        <Property>regKeyExists</Property>
        <Path>HKLM\SOFTWARE\MozillaPlugins\ K:@microsoft.com/GENUINE</Path>                             
        <Operator>-eq</Operator>
        <Value>true</Value>
    </Rule>
</Or>

...but which could have nodes other than <Rule> and <Or>, and could have values for Scope other than "node", I am trying to select just the Rule and Or nodes where scope=node. Currently I am using $nodes = $xml.SelectNodes("./*[@scope='node']") then looping with foreach ($node in $nodes) and checking $node.name so I can work with just the Rule and Or nodes. But my preference would be to do it with XPath. I have found reference to using or in predicate expressions, but only with attributes, not with node names.

like image 742
Gordon Avatar asked Feb 21 '26 19:02

Gordon


1 Answers

This XPath,

//*[self::Rule or self::Or][@scope='node']

will select all Rule or Or elements with @scope attribute values of 'node'.

like image 185
kjhughes Avatar answered Feb 23 '26 17:02

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!