Topic
10 replies Latest Post - ‏2010-07-02T04:39:54Z by SystemAdmin
NormanBird
NormanBird
5 Posts
ACCEPTED ANSWER

Pinned topic Xpath data displayhelp needed

‏2010-06-30T20:22:36Z |
I'm trying to use xpath to display data in a certain way.

I need data to display something like this:

Ascended Master
name1 name2 name3

Akashic Records
Earth-Library Solar-Library
.
.
.
xml file is attached, but its basically category->child->name
I need the look to display each category and show each category individual child items(yes i used name child)

code im using that doesn't do it:

<?php $xml = 

new SimpleXMLElement(file_get_contents(
"newage.xml")); ?> <?php   
// start the control loop using all categories foreach ($xml->xpath(
'//category/name') as $category) 
{ echo $category,
'<br>'; foreach ($xml->xpath(
'//category//child') as $child ) echo $child->name; echo 
"<br>"; echo 
"<br>"; 
}     
//end for each control loop   ?>


Now the above code displays each category dynamically which is good, but xpath is showing all childs for ALL the categories on each pass. I've been playing with this for over a week and I give up. Im hoping xpath has some "self" selecting terms I haven't found yet or something. As an added benefit anyone that helps land a solution I use, ill send you $20 via PayPal. :-)

thanks
Updated on 2010-07-02T04:39:54Z at 2010-07-02T04:39:54Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts
    ACCEPTED ANSWER

    Re: Xpath data displayhelp needed

    ‏2010-07-01T10:49:28Z  in response to NormanBird
    [0] Now the above code displays each category dynamically which is good, but xpath is showing all childs for ALL the categories on each pass.
    A quick scan of the attachment shows that name(s) are attributes of "category" and "child" elements, so I am not sure it shows anything at all.

    [1] Here is a minimal re-write for the purpose. (And I prefer xml/xhtml rules even for html, hence I close as well br's.)
    
    <?php $xml = 
    
    new SimpleXMLElement(file_get_contents(
    "newage.xml")); 
    // start the control loop using all categories foreach ($xml->xpath(
    '//category') as $category) 
    { echo $category[
    'name'].
    '<br />'; foreach ($category->xpath(
    'child') as $child ) 
    { echo $child[
    'name']; echo 
    '<br />'; echo 
    '<br />'; 
    } 
    }  
    //end for each control loop ?>
    
    • NormanBird
      NormanBird
      5 Posts
      ACCEPTED ANSWER

      Re: Xpath data displayhelp needed

      ‏2010-07-01T13:49:46Z  in response to SystemAdmin
      thanks for the reply tsuji.

      I ran code in dreamweaver as you had it and I got a blank screen. it seems your code is sending blank variable data, meaning we are selecting nodes with no data. I was able to play with your code and get it to work by replacing category("name") with category->name etc. and also I switched ($category->xpath('child') to $category->child

      this worked but only delivered one child for each category instead of dynamically listing what is actually there. like this:

      Akashic Records
      Earth-Library

      Ascended Masters
      Quan Yin

      in my sample xml file there is at least 2 child for each category. ill paste sample xml file below to show you:

      
      <data xmlns:xsi=
      "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=
      "c:\users\owner\websites\newage\newage.xsd"> <category> <name>Akashic Records</name> <description>It stores all the records of experiences in libraries. </description> <child> <name>Earth Library</name> <description>Stored experiences of Earth</description> <url>earthlibrary.html</url> <parent>Akashic Records</parent> <parent>spirit world</parent> </child> <child> <name>Solar Library</name> <description>Stored experiences of Eth Solar System</description> <url>solarlibrary.html</url> <parent>Akashic Records</parent> <parent>spirit world</parent> </child> </category> <category> <name>Ascended Masters</name> <description>Humans who have lived and ascended into higher realms. </description> <child> <name>Quan Yin</name> <description>Ascended master who Lived in Ancient China</description> <url>quan-yin.html</url> <parent>Ascended Masters</parent> <parent>spirit world</parent> </child> <child> <name>St. Germaine</name> <description>Ascended master who had many lives. One was Merlin</description> <url>stgermaine.html</url> <parent>Ascended Masters</parent> <parent>spirit world</parent> </child> </category> </data>     so it appears there is something 
      
      else that needs to be done or the looping needs to be tweeked, and thats where I have been unable to find a solution. I
      've actually gotten this far on my own before. just cant get the childs to display ALL childs dynamically. Im sensing xpath should be able to delive, since im so close.   thanks 
      
      for all the your help also.
      
      • SystemAdmin
        SystemAdmin
        746 Posts
        ACCEPTED ANSWER

        Re: Xpath data displayhelp needed

        ‏2010-07-01T14:19:49Z  in response to NormanBird
        Without looking too much in the detail of the message, I do not recognize the xml document. Name was never an element in your attachment. It is always an attribute under category or child.
        Please clarify, otherwise, I cannot help.
      • SystemAdmin
        SystemAdmin
        746 Posts
        ACCEPTED ANSWER

        Re: Xpath data displayhelp needed

        ‏2010-07-01T14:44:12Z  in response to NormanBird
        
        <?php $xml = 
        
        new SimpleXMLElement(file_get_contents(
        "newage.xml")); 
        // start the control loop using all categories foreach ($xml->xpath(
        '//category') as $category) 
        { echo $category->name.
        '<br />'; foreach ($category->xpath(
        'child') as $child ) 
        { echo $child->name; echo 
        '<br />'; echo 
        '<br />'; 
        } 
        }  
        //end for each control loop ?>
        

        ps: But I still need a statement from the op to the effect that two version documents contain generic difference, namely, the name attribute vs name element.
        • NormanBird
          NormanBird
          5 Posts
          ACCEPTED ANSWER

          Re: Xpath data displayhelp needed

          ‏2010-07-01T15:58:44Z  in response to SystemAdmin
          that may be the case that I sent you two versions of the xml file as i made changes and hoped that removing attributes would make it easier. I can use any one you feel better with, but for now lets just go with the version I pasted above without attributes.

          i think either way we are still up against it only showing one child I believe.
          thanks

          Norman
          • SystemAdmin
            SystemAdmin
            746 Posts
            ACCEPTED ANSWER

            Re: Xpath data displayhelp needed

            ‏2010-07-01T16:05:11Z  in response to NormanBird
            I need your statement, because this is a public forum and the attachment may change without notice. In any case, by the two versions I mean one as the attachment at the op's time and the second what you just posted explicitly not in the attachment form. In any case, I think this is clear.

            Run the php, have you not?
            • NormanBird
              NormanBird
              5 Posts
              ACCEPTED ANSWER

              Re: Xpath data displayhelp needed

              ‏2010-07-01T16:27:49Z  in response to SystemAdmin
              I did run the Php and It just shows the first child of each category and that the 2 that are listed. it should be dynamic and it appears to only select and display the first child not ALL childs of each category. it shows this:

              Akashic records
              Earth-Library

              Ascended Masters
              Quan Yin

              it should show this:

              Akashic records
              Earth-Library
              Solar-Library

              Ascended Masters
              Quan Yin
              St. Germain
              thanks

              Norman Bird
              www.DatabaseInDays.com
              www.chucklehut.com
              www.creationislove.com
              www.My-Instant-scheduling.com
              • SystemAdmin
                SystemAdmin
                746 Posts
                ACCEPTED ANSWER

                Re: Xpath data displayhelp needed

                ‏2010-07-01T17:11:58Z  in response to NormanBird
                I got this (literally html-wise)
                
                Akashic Records<br/>Earth Library<br/><br/>Solar Library<br/><br/>Ascended Masters<br/>Quan Yin<br/><br/>St. Germaine<br/><br/>
                

                which means effectively (taken out the artifact of br or double br)
                
                Akashic Records Earth Library Solar Library   Ascended Masters Quan Yin St. Germaine
                
                • NormanBird
                  NormanBird
                  5 Posts
                  ACCEPTED ANSWER

                  Re: Xpath data displayhelp needed

                  ‏2010-07-01T18:56:41Z  in response to SystemAdmin
                  You are correct, it does work. Thanks for the help.
                  If you provide your PayPal email I can send you the $20 I mentioned.

                  thanks,

                  Norman Bird
                  www.DatabaseInDays.com
                  www.chucklehut.com
                  www.creationislove.com
                  www.My-Instant-scheduling.com
                  • SystemAdmin
                    SystemAdmin
                    746 Posts
                    ACCEPTED ANSWER

                    Re: Xpath data displayhelp needed

                    ‏2010-07-02T04:39:54Z  in response to NormanBird
                    Don't mention it. Send to msf/rsf if you want to. Thank you for the question.