Simple xpath question

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

    Simple xpath question

    Dear all,
    supposed I have xml file
    <a>
    <b>
    <name>ggg</name>
    <c>10</c>
    <name>ddd</name>
    </b>

    <b>
    <name>zzz</name>
    <c>10</c>
    <name>yyyy</name>
    </b>
    <b>
    <name>zzz</name>
    <c>12</c>
    <name>xxxxx</name>
    </b>

    </a>
    Lets say I need to select all nodes where c=10 and print a path of names
    dim my_list as xmlnodelist =myRootNode.Sel ectNodes("//a/b[c='10']")
    Now I need to print a path of nodes name. The result should be:
    ---------------------
    ggg/ddd
    zzz/yyy
    --------------------
    Please help me. How can I do that? I have no idea how to get this info from
    my_list


  • Zhiv Kurilka

    #2
    Re: Simple xpath question

    Sorry, error in xml file. Should be:

    <root>
    <a>
    <name>ddd</name>
    <b>
    <name>ggg</name>
    <c>10</c>
    </b>
    </a>

    <a>
    <name>yyyy</name>
    <b>
    <name>zzz</name>
    <c>10</c>
    </b>
    </a>
    <a>
    <name>zzz</name>
    <b>

    <c>12</c>
    <name>xxxxx</name>
    </b>
    </a>

    </root>


    Comment

    • Marc Gravell

      #3
      Re: Simple xpath question

      Something like (C# - and I've been lazy using string concatenation):

      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml );
      foreach (XmlElement el in doc.SelectNodes ("/root/a/b[c='10']"))
      {
      string name = el.SelectSingle Node("name").In nerText, path = "";
      foreach (XmlElement pathEl in
      el.SelectNodes( "ancestor-or-self::*/name"))
      {
      path += "/" + pathEl.InnerTex t;
      }
      Debug.WriteLine (name + ": " + path);
      }

      Marc


      Comment

      • Zhiv Kurilka

        #4
        Re: Simple xpath question

        Thanks just what I needed
        "Marc Gravell" <marc.gravell@g mail.comschrieb im Newsbeitrag
        news:%23gPaq0kt GHA.3964@TK2MSF TNGP04.phx.gbl. ..
        Something like (C# - and I've been lazy using string concatenation):
        >
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml );
        foreach (XmlElement el in doc.SelectNodes ("/root/a/b[c='10']"))
        {
        string name = el.SelectSingle Node("name").In nerText, path = "";
        foreach (XmlElement pathEl in
        el.SelectNodes( "ancestor-or-self::*/name"))
        {
        path += "/" + pathEl.InnerTex t;
        }
        Debug.WriteLine (name + ": " + path);
        }
        >
        Marc
        >

        Comment

        Working...