Topic
  • 5 replies
  • Latest Post - ‏2009-09-22T11:58:25Z by SystemAdmin
SystemAdmin
SystemAdmin
746 Posts

Pinned topic Xpath Does Not Correlate Parent With Variable Number Of Children

‏2009-08-30T14:21:44Z |
Hello,
I have to parse an xml document that contains names of sports teams , but these sport teams tags are included in league tags , so inside a league can be a variable number of teams (1, 2 etc.). Like this :

<league>
<name>NCAA</name>
<team>
<name>Akron </name>
<decimalodds>2</decimalodds>
</team>
<team>
<name>Ohio </name>
<decimalodds>1.833333</decimalodds>
</team>
</league>
<league>
<name>AFC</name>
<team>
<name>Indianapolis Colts</name>
<decimalodds>1.0625</decimalodds>
</team>
<team>
<name>Jacksonville Jaguars</name>
<decimalodds>11</decimalodds>
</team>
<team>
<name>Tennessee Titans</name>
<decimalodds>11</decimalodds>
</team>
</league>
.............

So first league has 2 teams inside (2 children) and second has 3 teams , but the number of leagues can also be variable from one xml file to another.

I need to get directly name of teams using xpath and put it in array but also get the league for each team , which , in terms of xpath , means the parent of each children, like this :

$feed = array( 'team_name' => '../league/team/name'
'league_name' => '//team/../name ',

};

So , my array should have: $feed[0]= 'Akron'
$feed[0]= 'NCAA'

$feed[1]= 'Ohio'
$feed[1]= 'NCAA'

$feed[2]= 'Indianapolis Colts'
$feed[2]= 'AFC'

$feed[3]= 'Jacksonville Jaguars'
$feed[3]= 'AFC'

$feed[4]= 'Tennessee Titans'
$feed[4]= 'AFC'

but I don't !!
Instead i have : $feed[0]= 'Akron'
$feed[0]= 'NCAA'

$feed[1]= 'Ohio'
$feed[1]= 'AFC'

$feed[2]= 'Indianapolis Colts'
$feed[2]= ''

$feed[3]= 'Jacksonville Jaguars'
$feed[3]= ''

$feed[4]= 'Tennessee Titans'
$feed[4]= ''

So what can I do ? Is there any solution to make xpath corelate children witj parents and treat them separately ??

Thank you.
Updated on 2009-09-22T11:58:25Z at 2009-09-22T11:58:25Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath Does Not Correlate Parent With Variable Number Of Children

    ‏2009-08-30T14:29:02Z  
    Sorry , about the array i meant

    $feed [0]= 'Akron'
    $feed [0]= 'NCAA'

    $feed [1]= 'Ohio'
    $feed[1]= 'NCAA'

    $feed [2]= 'Indianapolis Colts'
    $feed[2]= 'AFC'

    $feed [3]= 'Jacksonville Jaguars'
    $feed[3]= 'AFC'

    $feed [4]= 'Tennessee Titans'
    $feed[4]= 'AFC'

    but I don't !!
    Instead i have :

    $feed [0]= 'Akron'
    $feed [0]= 'NCAA'

    $feed [1]= 'Ohio'
    $feed[1]= 'AFC'

    $feed [2]= 'Indianapolis Colts'
    $feed[2]= ''

    $feed [3]= 'Jacksonville Jaguars'
    $feed[3]= ''

    $feed [4]= 'Tennessee Titans'
    $feed[4]= ''
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath Does Not Correlate Parent With Variable Number Of Children

    ‏2009-08-30T14:30:56Z  
    Sorry , about the array i meant

    $feed [0]= 'Akron'
    $feed [0]= 'NCAA'

    $feed[1]= 'Ohio'
    $feed[1]= 'NCAA'

    $feed[2]= 'Indianapolis Colts'
    $feed[2]= 'AFC'

    $feed[3]= 'Jacksonville Jaguars'
    $feed[3]= 'AFC'

    $feed[4]= 'Tennessee Titans'
    $feed[4]= 'AFC'

    but I don't !!
    Instead i have :

    $feed[0]= 'Akron'
    $feed[0]= 'NCAA'

    $feed[1]= 'Ohio'
    $feed[1]= 'AFC'

    $feed[2]= 'Indianapolis Colts'
    $feed[2]= ''

    $feed[3]= 'Jacksonville Jaguars'
    $feed[3]= ''

    $feed[4]= 'Tennessee Titans'
    $feed[4]= ''
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath Does Not Correlate Parent With Variable Number Of Children

    ‏2009-08-30T14:33:39Z  
    Sorry , about the array i meant

    $feed [0]= 'Akron'
    $feed [0]= 'NCAA'

    $feed[1]= 'Ohio'
    $feed[1]= 'NCAA'

    $feed[2]= 'Indianapolis Colts'
    $feed[2]= 'AFC'

    $feed[3]= 'Jacksonville Jaguars'
    $feed[3]= 'AFC'

    $feed[4]= 'Tennessee Titans'
    $feed[4]= 'AFC'

    but I don't !!
    Instead i have :

    $feed[0]= 'Akron'
    $feed[0]= 'NCAA'

    $feed[1]= 'Ohio'
    $feed[1]= 'AFC'

    $feed[2]= 'Indianapolis Colts'
    $feed[2]= ''

    $feed[3]= 'Jacksonville Jaguars'
    $feed[3]= ''

    $feed[4]= 'Tennessee Titans'
    $feed[4]= ''
    I dont know what's wrong with website , each element in array should be like $feed[0] , $feed[0] etc
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath Does Not Correlate Parent With Variable Number Of Children

    ‏2009-08-30T15:26:13Z  
    I dont know what's wrong with website , each element in array should be like $feed[0] , $feed[0] etc
    I suspect I know what you're getting at and I share your frustration with this awful forum software. Putting code in { code } tags helps sometimes but not always.

    I would first select the nodeset
    
    //league/team
    
    and then for each of these nodes, put the value of string(name) in one array, and the value of string(../name) in the other.

    I can't translate that into your chosen programming language because it's not a language I use.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath Does Not Correlate Parent With Variable Number Of Children

    ‏2009-09-22T11:58:25Z  
    It is not clear the working language for the question. I suppose php just for some resemblance. Also, it is not clear exactly how you want the construction of $feed. Since parent being mentioned, I would use a version using precisely parent axis for illustration how it be done. To use xpath, I would use php's SimpleXML extension.
    
    <?php $s = <<<xsource <leagues> <league> <name>NCAA</name> <team> <name>Akron </name> <decimalodds>2</decimalodds> </team> <team> <name>Ohio </name> <decimalodds>1.833333</decimalodds> </team> </league> <league> <name>AFC</name> <team> <name>Indianapolis Colts</name> <decimalodds>1.0625</decimalodds> </team> <team> <name>Jacksonville Jaguars</name> <decimalodds>11</decimalodds> </team> <team> <name>Tennessee Titans</name> <decimalodds>11</decimalodds> </team> </league> </leagues> xsource;   $doc=simplexml_load_string($s); $feed=array(); foreach ($doc->xpath(
    '//team') as $child) 
    { foreach ($child->xpath(
    'parent::*') as $parent) 
    { $feed[
    "$child->name"]=$parent->name; 
    } 
    } foreach ($feed as $key=>$value) 
    { echo $key.
    ",".$value.
    "\n"; 
    } ?>