XPath

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil

    XPath

    I'm trying to get a list of nodes using the XMLNodeList.Sel ectNodes( xpath ) method. I want a list of criteria nodes where the attribute notModifiable is false or doesn't exist. Anyone know how to do this? So far I can grab the nodes with notModifiable equal false but I don't know how to do the second bit. My statement is like below

    XmlNodeList criteriaNodeLis t = groupNode.Selec tNodes("criteri a[attribute::notm odifiable != \"true\"]");

    I need to append something to this XPath statement to get the nodes which do not contain the attribute notModifiable
  • Martin Honnen

    #2
    Re: XPath



    Neil wrote:
    [color=blue]
    > I'm trying to get a list of nodes using the XMLNodeList.Sel ectNodes(
    > xpath ) method. I want a list of criteria nodes where the attribute
    > notModifiable is false or doesn't exist. Anyone know how to do this?
    > So far I can grab the nodes with notModifiable equal false but I
    > don't know how to do the second bit. My statement is like below.
    >
    > XmlNodeList criteriaNodeLis t =
    > groupNode.Selec tNodes("criteri a[attribute::notm odifiable !=
    > \"true\"]");
    >
    > I need to append something to this XPath statement to get the nodes
    > which do not contain the attribute notModifiable[/color]

    With the example document being

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <criteria notModifiable=" true" />
    <criteria notModifiable=" false" />
    <criteria />
    </root>

    you need
    criteria[not(@notModifia ble) or @notModifiable = 'false']

    the following sample C# program extracts those

    using System;
    using System.Xml;

    public class Test20040408 {
    public static void Main (string[] args) {
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Loa d(@"test2004040 8.xml");
    XmlNodeList criterias = xmlDocument.Sel ectNodes(
    "//criteria[not(@notModifia ble) or @notModifiable = 'false']");
    foreach (XmlNode criteria in criterias) {
    Console.WriteLi ne(criteria.Out erXml);
    }
    }
    }
    --

    Martin Honnen


    Comment

    • Daniel Cazzulino [MVP XML]

      #3
      Re: XPath

      XPath spec. says empty node lists (i.e. a node that does not exist for the
      test) are converted to false. So you only need the following:

      XmlNodeList criteriaNodeLis t =
      groupNode.Selec tNodes("criteri a[not(attribute:: notmodifiable)]");

      the not() function implicitly converts its parameter to a boolean.
      Therefore, if the attribute exists, its value is converted to a boolean. If
      it doesn't, it's converted to false.

      --
      Daniel Cazzulino [MVP XML]
      Clarius Consulting SA


      "Neil" <ndglover@urban science.com> wrote in message
      news:5E1753E7-2AC0-41A3-85AD-599A0139D441@mi crosoft.com...[color=blue]
      > I'm trying to get a list of nodes using the XMLNodeList.Sel ectNodes([/color]
      xpath ) method. I want a list of criteria nodes where the attribute
      notModifiable is false or doesn't exist. Anyone know how to do this? So far
      I can grab the nodes with notModifiable equal false but I don't know how to
      do the second bit. My statement is like below.[color=blue]
      >
      > XmlNodeList criteriaNodeLis t =[/color]
      groupNode.Selec tNodes("criteri a[attribute::notm odifiable != \"true\"]");[color=blue]
      >
      > I need to append something to this XPath statement to get the nodes which[/color]
      do not contain the attribute notModifiable


      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.665 / Virus Database: 428 - Release Date: 23/04/2004


      Comment

      Working...