Select XML Nodes by Parent attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdrianGawrys
    New Member
    • Nov 2007
    • 12

    #1

    Select XML Nodes by Parent attribute

    Hi Guys,

    I have an xml similar to this one:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <SystemUpdate ForceUpdate="false" >
      <A ForceUpdate="false">
        <Book name="Black" />
        <Book name="BlackRed" />
        <Book name="Red" />
        <Book name="Green" ForceUpdate="true"/>
      </A>
      <B ForceUpdate="true">
         <Book name="Old" />
         <Book name="New"/>
      </B>
    </CapUpdate>
    Can someone help me out and explain whats the simplest way of selecting nodes that have it's own or parent node's attribute: ForceUpdate set to true.

    Ie.: SystemUpdate's ForceUpdate is set to true = that will select all Book nodes,
    A node's ForceUpdate set to true = this will select Black,BlackRed, Red and Green Book nodes.
    B node's ForceUpdate set to true = this will select "Old" and "New" Book nodes.
    So in the quoted xml example selected nodes would be: Green,Old and New.

    Hope I explained it correctly.
    I was trying to find something that works similar but had no luck.

    Thanks in advance.
    Adrian
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well in the javascript world, you can use an XPATH statement to do that.
    I think the XML objects in .NET have similar abilities?

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      You can use XmlReader, to get values of your attributes:

      Code:
      XmlReader reader = new XmlTextReader(inputStream);
      reader.ReadToFollowing("A");
      string attributeValue = reader.GetAttribute("ForceUpdate");
      XmlReader.GetAt tribute will return 'true' or 'false' (or null if that attribute does not exist for the current element).

      Comment

      • AdrianGawrys
        New Member
        • Nov 2007
        • 12

        #4
        Thanks for your responses guys.

        @vekipeki - I should have mentioned that I am able to select a node and test for attribute's value, but I am more interested in some sort of xpath statement that will get select all nodes that have a parent node with attribute ForceUpdate set to 'true' and store them into XmlNodeList variable.
        @plater - would you be able to sketch statement in Javascript that does that?

        Thanks,

        Adrian

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Well I can never keep my head wrapped around XPath to be sure, but I really liked the examples found here:


          Example #6 looks most like what you want for selecting by an attribute.
          I think you can combine it with /*

          I came up with:
          //*[@ForceUpdate="t rue"]/*

          Which i *think* means "select all nodes that are children of any nodes with attribute ForceUpdate = true"

          Comment

          • AdrianGawrys
            New Member
            • Nov 2007
            • 12

            #6
            @Plater you have no idea how grateful I am for the link to examples you've sent.
            I was never too good with XPath but that helped me to understand it.
            Currently I've achieved what I wanted with two XPath queries - one for all book nodes that have 'ForceUpdate' attribute set to true and another query that selects all Book nodes whose Parent nodes have ForceUpdate set to true.

            What I need to do now is just to combine them into one.

            I will post my XPath query shortly.

            Thanks again for your help!

            Adrian

            Comment

            Working...