10 Replies Latest reply on Apr 3, 2009 8:37 AM by alice_data

    How to Access the Parent Node Attribute of a Child?

    alice_data Level 1
      Hi,

      I am trying to accomplish something simple here, by accessing the parent node's attribute value of a child value. I have the date provided here, but I am trying to find out the name of the event of the date, which the name of the event is the attribute of the date's parent.

      I have the code here as in the following, but I kept getting #1009 term not defined error at the line with parent() in it. Could anyone please guide me on how to fix this?

      Thanks in advance.

      Alice
        • 1. Re: How to Access the Parent Node Attribute of a Child?
          Peter Hahmann Level 1
          Alice,

          What is happening. When you get resultC, it is a part of your XML. When you choose its parent(), there is nothing, so you wont be able get the attribute.

          What exactly you plan to so, so i could help you.

          Regards,
          peter
          • 2. Re: How to Access the Parent Node Attribute of a Child?
            alice_data Level 1
            Hi, Peter:

            What I am trying to do here is, given that I have a selected date that equals to a certain value, I can use that value to query through the XML back to its "parent" to find its attribute. In this case, given that resultC = searchResult..book.schedule_item.(date == '2009/03/22'), I would like to be able to extract BarCoding from the XML as shown in the following:

            <schedule_item type="BarCoding"> //What I want to extract
            <date>2009/03/20</date>
            <date>2009/03/22</date> //The selected value
            </schedule_item>

            Is this very difficult? I am surprised that when I tried to print out resultC, I get nothing too.
            Thanks for your help.

            Alice
            • 3. Re: How to Access the Parent Node Attribute of a Child?
              Peter Hahmann Level 1
              Alice,

              Instead of creating a new xlllist when searching for something you could loop trough its descendants. So you would never "lose" the parent. Did you get what i'm meaning? If not, I could send you an example.

              Regards,
              Peter
              • 4. Re: How to Access the Parent Node Attribute of a Child?
                alice_data Level 1
                Hi, Peter:

                I got this to work finally, but I am not sure if this is the easiest way to do it. I have attached it at the end of the message, but I would love to see the example if it is different from how I do it.

                Thanks.
                • 5. How to Access the Parent Node Attribute of a Child?
                  Peter Hahmann Level 1
                  Try this one:


                  Regards,
                  Peter
                  • 6. Re: How to Access the Parent Node Attribute of a Child?
                    Level 7

                    "Peter Hahmann" <webforumsuser@macromedia.com> wrote in message
                    news:gr2p15$no6$1@forums.macromedia.com...
                    > Alice,
                    >
                    > Instead of creating a new xlllist when searching for something you could
                    > loop
                    > trough its descendants. So you would never "lose" the parent. Did you get
                    > what
                    > i'm meaning? If not, I could send you an example.

                    Don't be ridiculous. Each element returned by and xml expression
                    "remembers" its original place in the tree.


                    • 7. Re: How to Access the Parent Node Attribute of a Child?
                      Level 7

                      "alice_data" <webforumsuser@macromedia.com> wrote in message
                      news:gr2r6g$qbn$1@forums.macromedia.com...
                      > Hi, Peter:
                      >
                      > I got this to work finally, but I am not sure if this is the easiest way
                      > to
                      > do it. I have attached it at the end of the message, but I would love to
                      > see
                      > the example if it is different from how I do it.

                      Alice, have you seen this article:

                      http://dispatchevent.org/roger/as3-e4x-rundown/
                      ?


                      • 8. Re: How to Access the Parent Node Attribute of a Child?
                        Level 7

                        "alice_data" <webforumsuser@macromedia.com> wrote in message
                        news:gr2fkh$cre$1@forums.macromedia.com...
                        > Hi,
                        >
                        > I am trying to accomplish something simple here, by accessing the parent
                        > node's attribute value of a child value. I have the date provided here,
                        > but I
                        > am trying to find out the name of the event of the date, which the name of
                        > the
                        > event is the attribute of the date's parent.
                        >
                        > I have the code here as in the following, but I kept getting #1009 term
                        > not
                        > defined error at the line with parent() in it. Could anyone please guide
                        > me on
                        > how to fix this?
                        >
                        > Thanks in advance.
                        >
                        > Alice
                        >
                        > Actionscript Snippet:
                        >
                        > var resultC:XMLList;
                        > resultC = searchResult..book.schedule_item.date;
                        > var resultD:XMLList;
                        >
                        > /*The following two lines kept giving me bad #1009 errors
                        >
                        > // resultD = resultC.parent().@type;
                        > // trace("This is D " + resultD + "\n");
                        >
                        > */ End of error
                        >
                        > var resultC_collection:XMLListCollection = new
                        > XMLListCollection(resultC.*);
                        >
                        > //Debug purpose, print out the XML string of the selected result
                        > trace("This is the result: " + resultC);
                        >
                        > XML:
                        >
                        > <schedule_item type="BarCoding">
                        > <date>2009/03/20</date>
                        > <date>2009/03/22</date>
                        > </schedule_item>

                        I think your issue is that resultC is an XMLList. To get to the parent, you
                        need to get to ONE element in the list. Even though it is a list containing
                        one element, that is not the same thing as one element.

                        Try

                        //note I don't see book in your xml, so I doubt that is correct
                        resultC = searchResult.child('book').child('schedule_item').child('date');

                        resultD=resultC[0].parent().attribute('type');

                        Note that the syntax you used will break if any element does not have a
                        child or attribute matching your query, so I used the more verbose syntax.

                        HTH;

                        Amy


                        • 9. Re: How to Access the Parent Node Attribute of a Child?
                          Peter Hahmann Level 1
                          Okay, sorry for providing wrong information.
                          • 10. Re: How to Access the Parent Node Attribute of a Child?
                            alice_data Level 1
                            Hi, Amy and Peter:

                            Thanks for your help. I have to admit that I didn't see this article before while I was browsing the web. However, I did get the code eventually to work (before I saw your code).

                            Peter, even though there are some of the wrong information you have provided, I got some takeaways from the example you provided. :) That was what I got me started to work on the version I had.

                            Here is the working version of my code, and I would try to implement what Amy mentioned in the previous post.

                            Thanks for your help.