Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Info Coronavirus/Vaccinare vs Fake News

Reprezentanța B.B.C. (Brown, Bove...

Analizele Medicale, intre fericir...

Recomandare carcasa si sursa cu pwm
 Cum s-ar putea face plafonarea pr...

Preparing automatic repair diagno...

Planul Samsung pentru urmatorii 5...

videoproiector vs oricare display
 Produse comandate din china (wish...

probleme mașina de spalat rufe

Magazine care vand bere la keg

Proiect ciudat legat de metaverse...
 Daniel Fenechiu: Nu vom aște...

Transfer in acelasi pc cu FTP

Și daca industria europeana ...

Alimentare camera ip distanta 50m
 

Xpath not corelate Parent with multiple children tags

- - - - -
  • Please log in to reply
14 replies to this topic

#1
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
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 ['team_name'] [0]= 'Akron'
$feed ['league_name'] [0]= 'NCAA'

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

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

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

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

but I don't !!
Instead i have :

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

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

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

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

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


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

Thank you.

#2
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007

View Postirinacst, on 30th August 2009, 17:44, said:

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 ['team_name'] [0]= 'Akron'
$feed ['league_name'] [0]= 'NCAA'

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

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

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

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

but I don't !!
Instead i have :

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

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

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

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

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


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

Thank you.
I understand what you what but i need too see more of your script. You have to browse all team name children from the xml file with a for loop and you may use ../ to get parent node content [../../name].

#3
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009

View PostNLC, on 31st August 2009, 17:12, said:

I understand what you what but i need too see more of your script. You have to browse all team name children from the xml file with a for loop and you may use ../ to get parent node content [../../name].


Buna :)

Multumesc de raspuns, scuze ca am postat in engleza dar am facut asta pentru ca am postat si pe forumuri in engleza si nu am mai avut timp sa scriu si in romana.

Codul parser-uui meu este urm clasa php :

include_once("mysql.class.php");

class XmlParser
{
  $mysqlCon;

public function __construct()
{
         $mysqlCon= new MysqlCon();
}

public function parse($xmlurl,$feed)
{
      // read the xml source as string
      $str = file_get_contents($xmlurl);
  
      // load the string as xml object
      $xml = simplexml_load_string($str);
  
      // initialize the return array
      $result = array();

     // parse the xml nodes
     foreach($feed as $key => $xpath)
     {
       $values = $xml->xpath("{$xpath}");
       if ( is_array($values) )
       {
     foreach($values as $value)
     {
           $result[$key][] = (string)$value;
         }
   }
      else { $result[$key][] = (string)$values;
           }
     }
    

     return $result;
  }
  
   public function xml_into_db($urls, $xml_structure)
   {
  
   foreach ($urls as $key => $xmlurl)
   {
      $data= $this->parse( $xmlurl, $xml_structure );
    
    //print_r($data);
  
      $n = count( $data['team_name'] );
  echo '<br> nr teams: '.$n;

      for ($i=0;$i<$n; $i++)
      {  
          $sql="INSERT into teams values ('".$data ['team_name'] [$i]."','".$data ['league_name'] [$i]."')";
          $mysqlCon->insert($sql);
  
       }

    }
    
}//end function xml_to_db


}

Clasa mysql face adaugarea in bd, iar parametrii care dau clasei XmlParser sunt linkul catre fis xml si array-ul de care am spun mai sus ( $feed ).


Problema e ca xpath trateaza independent cele 2 variabile , team_name si league_name, adica daca am 5 echipe si 2 ligi , liga 1 cu (echipa1, echipa2, echipa3) si liga 2 cu (echipa 4, echipa 5 ) el imi va pune in array :

$feed['team_name'][0]='echipa1';
$feed['league_name'][0]='liga1';

$feed['team_name'][1]='echipa2';
$feed['league_name'][1]='liga2';

$feed['team_name'][2]='echipa3';
$feed['league_name'][2]=''';

$feed['team_name'][3]='echipa4';
$feed['league_name'][3]='';

$feed['team_name'][4]='echipa5';
$feed['league_name'][4]='';


Deci nu el asociaza cum trebuie, nu respecta structura de arbore, le trateaza separat.

Asadar, stii cumva cum trebuie sa declar path-urile in array:

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

};

pentru ca eu am incercat in multe moduri si nu amers ?

Si trebuie sa mai precizez ca nu vreau sa tratez cu cod php xml-ul , adica sa fac loop pt parinte si a gasesc copii, pentru ca asta ar insemna sa particualrizez pentru structura asta de fisier si eu am mai multe tipuri de xml-uri.
Clasa XmlParser trebuie sa raman generala, pentru orice fel de structura., doar ca vor exista mai mulre array-uri , gen $feed1, $feed2, pentru fiecare tip de structura si eu voi folosi pe cel care trebuie.
Deci ideea ar fi sa extrag numele ligii pentru fiecare echipa de la inceput, prin array-ul respectiv.

So, any idea ?  :)

Edited by irinacst, 31 August 2009 - 18:21.


#4
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007
ai incercat cu //team/name/../name ?

#5
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009

View PostNLC, on 31st August 2009, 21:37, said:

ai incercat cu //team/name/../name ?


hmmm...nu stiu sigur , am incercat multe variante , dar incerc acum....

#6
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
incercat acum, nu merge , nu vrea sa le asocieze....

alta idee ? :)

sau nu stiu , poate ar trebui sa implementez si ceva in codul php ca sa rezolv problema asta , dar nu stiu cum sa fac ca sa pastrez totusi o anumita generalitate...

orice idee e binevenita, sau exemple de ceva ce ai facut tu similar  :)

ms pt raspunsuri

#7
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007

View Postirinacst, on 31st August 2009, 22:25, said:

incercat acum, nu merge , nu vrea sa le asocieze....

alta idee ? :)

sau nu stiu , poate ar trebui sa implementez si ceva in codul php ca sa rezolv problema asta , dar nu stiu cum sa fac ca sa pastrez totusi o anumita generalitate...

orice idee e binevenita, sau exemple de ceva ce ai facut tu similar  :)

ms pt raspunsuri
nu am facut niciodata in php asa ceva... dar ai putea incerca cu xsl si chiar din php folosind functii xsl_*

#8
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
aha , da am mai citit putin despre xsl dar n-am avut rabdare pana acum sa incerc sa fac ceva , mi se pare mult mai complicat ca si cod....

poti sa-mi spui pentru exemplul asta simplu pe care l-am dat eu cum s-ar face in xsl , in loc de xpath ?

thx

#9
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007

View Postirinacst, on 31st August 2009, 23:30, said:

aha , da am mai citit putin despre xsl dar n-am avut rabdare pana acum sa incerc sa fac ceva , mi se pare mult mai complicat ca si cod....

poti sa-mi spui pentru exemplul asta simplu pe care l-am dat eu cum s-ar face in xsl , in loc de xpath ?

thx
ar fi ceva de genul

<xsl:template match="/">
   <xsl:for-each select="//league">
	 <xsl:value-of select="./name" />
	 <xsl:for-each select="./team">
		  <xsl:value-of select="./name" />
	 </xsl:for-each>
   </xsl:for-each>
</xsl:template>
in mare cam in asta consta: parcurgi fisierul liga cu liga si returnezi numele acestora si pentru fiecare liga parcurgi copii [echipele] si returnezi numele acestora. ceea ce incerci tu in php fara o parcurgere macar pentru fiecare copil al ligi e imposibil, cred...

Edited by NLC, 31 August 2009 - 23:16.


#10
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
am inteles , acum pare simplu dupa cum ai prezentat u :)

dar problema e ca eu cu xpath puneam toate datele intr-un array si apoi le preluam usor in php....

in xsl cum preiau valorile?

si inca ceva , pentru xsl trebuie facute setari speciale in php.ini sau ceva d genu?

#11
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007

View Postirinacst, on 1st September 2009, 00:29, said:

am inteles , acum pare simplu dupa cum ai prezentat u :)

dar problema e ca eu cu xpath puneam toate datele intr-un array si apoi le preluam usor in php....

in xsl cum preiau valorile?

si inca ceva , pentru xsl trebuie facute setari speciale in php.ini sau ceva d genu?
nu am lucrat niciodata cu php si xsl. cauta ceva tutorial pe net sa vezi cum sa folosesti xsl din php. poate revin maine cu ajotor daca nu ai reusit :)

#12
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
ok , o sa caut , ms de ajutor :)

#13
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
am cautat tutoriale dar se pare ca xsl permite convertirea xml in html , dar nu prea vad cum pot sa obtin intr-un vector valorile din xml....

#14
NLC

NLC

    Junior Member

  • Grup: Members
  • Posts: 31
  • Înscris: 12.01.2007

View Postirinacst, on 1st September 2009, 20:44, said:

am cautat tutoriale dar se pare ca xsl permite convertirea xml in html , dar nu prea vad cum pot sa obtin intr-un vector valorile din xml....
incerca cu functia asta

#15
irinacst

irinacst

    New Member

  • Grup: Members
  • Posts: 9
  • Înscris: 30.08.2009
pai functia asta am folosit si eu in scriptul meu :
// load the string as xml object
$xml = simplexml_load_string($str);

remember?

Deci cu xpath nu merge ce vreau eu , merge cu xsl , cum ai zis u , pentru ca xsl e in stare sa faca loop (for each) pe elemente folosindu-se doar de  limbajul propriu (ssi nu de limbajul php cum face xpath)  dar acum ramane sa vedem cum preiau datele din template-ul xsl intr-un array in php ......

Edited by irinacst, 02 September 2009 - 06:09.


Anunturi

Bun venit pe Forumul Softpedia!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate