Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Come prendo solo il primo elemento??

    Ciao a tutti,
    Vi riporto un frammento del file xml che voglio interrogare con Xpath:

    <?xml version="1.0" encoding="UTF-8"?>
    <WebSite
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.dia.uniroma3.it/~volpi/files/database"
    xsi:schemaLocation="http://www.dia.uniroma3.it/~volpi/files/database
    /afs/vn.uniroma3.it/user/v/volpi/public/public_html/files/database/database.xsd">

    <ResearchGroup picture="http://www.dia.uniroma3.it/~compunet/images/compunet_banner.gif">
    <NameGroup>Computer Networks Research Group</NameGroup>
    <Description>computer networks</Description>
    <Faculty>www.dia.uniroma3.it</Faculty>
    <Department>www.web.dia.uniroma3.it</Department>
    <University>www.uniroma3.it</University>
    <CurrentMember>238651</CurrentMember>
    <CurrentMember>238652</CurrentMember>
    <CurrentMember>000001</CurrentMember>
    <PastMember>238650</PastMember>
    <PastMember>000002</PastMember>
    <Project>netkit</Project>
    <Project>hermes</Project>
    <Coordinator>1</Coordinator>

    <Publication>about netkit</Publication>
    <Publication>Visualization of the Autonomous Systems Interconnections with HERMES</Publication>

    <Publication>IPv6-in-IPv4 tunnel discovery: methods and experimental results</Publication>
    <Publication>bgplay</Publication>

    <Publication>Algorithms for the Inference of the Commercial Relationships between Autonomous Systems: Results Analysis and Model Validation</Publication>
    </ResearchGroup>

    <ResearchGroup picture="http://www.inf.uniroma3.it/research/GD.html">
    <NameGroup>Graph Drawing</NameGroup>
    <Description>
    Algorithm Engineering Research Group
    </Description>
    <Faculty>http://www.dia.uniroma3.it/research/ACG.html</Faculty>
    <Department>http://www.web.dia.uniroma3.it/</Department>
    <University>http://www.uniroma3.it</University>
    <CurrentMember>1</CurrentMember>
    <Project>netkit</Project>
    </ResearchGroup>
    Con questo script estrapolo le informazioni:

    Codice PHP:

    <?php
        $real_path 
    realpath("./files/database/database.xml");
        if(!
    $domdb domxml_open_file($real_path)){
          die(
    "Error while parsing the document");
          }
        else {
            echo 
    "<CENTER><H3>Xpath Test execute.</H3></CENTER>";
        }
        
    $xpath $domdb->xpath_new_context();
        
    xpath_register_ns($xpath'my',"http://www.dia.uniroma3.it/~volpi/files/database");
    ?>    

    <?php
      $query 
    "/my:WebSite/my:ResearchGroup/my:NameGroup/text()";
      
    $xpath_nodeset $xpath->xpath_eval($query);
      
    $names = array();
      foreach (
    $xpath_nodeset->nodeset as $iterator) {
       echo 
    $names[] = $iterator->get_content()."
    "
    ;
    }
    ?>
    ragazzi volevo chiedere:
    1. ma come faccio a estrapolare soltanto il primo elemento senza usare il foreach??
    Mi spiego meglio, con il foreach mi stampa:
    "Computer Networks Research Group"
    "Graphic Drawing"
    Ma se voglio soltanto il primo???
    non ditemi con l'indice dell'array perchè così ogni volta devo sapere con certezza la posizione dell'elemento.

    Aspetto consigli...Vi ringrazio anticipatamente.
    Saluti Simone V.
    L'importante no è quello che trovi alla fine di una corsa.
    L'importante è quello che provi mentre corri.
    ----

  2. #2
    se vuoi sempre e solo il primo basta mettere un break
    Codice PHP:
    <?php 
      $query 
    "/my:WebSite/my:ResearchGroup/my:NameGroup/text()"
      
    $xpath_nodeset $xpath->xpath_eval($query); 
      
    $names = array(); 
      foreach (
    $xpath_nodeset->nodeset as $iterator) { 
       echo 
    $names[] = $iterator->get_content()."
    "

      break;

    ?>
    Planet Earth is blue and there's nothing I can do

  3. #3

  4. #4
    Originariamente inviato da Sandro M.
    se vuoi sempre e solo il primo basta mettere un break
    Codice PHP:
    <?php 
      $query 
    "/my:WebSite/my:ResearchGroup/my:NameGroup/text()"
      
    $xpath_nodeset $xpath->xpath_eval($query); 
      
    $names = array(); 
      foreach (
    $xpath_nodeset->nodeset as $iterator) { 
       echo 
    $names[] = $iterator->get_content()."
    "

      break;

    ?>
    non voglio sempre il primo elemento ma semplicemente stamparne uno soltanto!
    per esempio voglio stampare soltanto il secondo...potete aiutarmi.

    grazie
    L'importante no è quello che trovi alla fine di una corsa.
    L'importante è quello che provi mentre corri.
    ----

  5. #5
    Originariamente inviato da nicola75ss
    http://se.php.net/manual/it/function.array-shift.php
    interessante funzione da tenere a portata di mano per una eventuale mandragata!!!!

    ma se mi serve per esempio il 5 elemento del tag NameGroup anche se in questo caso come faccio?
    L'importante no è quello che trovi alla fine di una corsa.
    L'importante è quello che provi mentre corri.
    ----

  6. #6
    dici che non vuoi il primo, che non vuoi usare l'indice dell'array, ...quindi?
    proviamo cosi
    Codice PHP:
    <?php 
      $query 
    "/my:WebSite/my:ResearchGroup/my:NameGroup/text()"
      
    $xpath_nodeset $xpath->xpath_eval($query); 
      
    $names = array();
      
    $i=0;//contatore
      
    $y=5;// da sostituire con il numero che vuoi
      
    foreach ($xpath_nodeset->nodeset as $iterator) {
            if (
    $i==$y) {//controllo tra contatore e tuo indice
                 
    echo $names[] = $iterator->get_content()."
    "

                break 
    1;//dopo la stampa blocca il foreach
          
    }
      
    $i++; //incrementa il contatore

    ?>
    ...ma cosi è simile a usare l'indice dell'array anzi è + scomodo
    Planet Earth is blue and there's nothing I can do

  7. #7
    Ti ringrazio intanto per la risposta, cmq sì è come usare l'indice...perchè
    Codice PHP:
    $y=5;// da sostituire con il numero che vuoi 
    se in futoro aggiungerò elementi al file xml dovrò ricordarmi di aumentare $y, e questo molto scomodo...non vi sembra?!
    L'importante no è quello che trovi alla fine di una corsa.
    L'importante è quello che provi mentre corri.
    ----

  8. #8
    Codice PHP:
    <?php
      $query 
    "/my:WebSite/my:ResearchGroup/my:NameGroup[0]/text()";
      
    $xpath_nodeset $xpath->xpath_eval($query);
      
    $it[] = $xpath_nodeset->nodeset; <- QUI
      
    echo $it->get_content();
    ?>
    ragazzi ho provato a stamparare soltanto il primo elemento, esprimendolo nella query.
    Sono sicuro che la sintassi della query è giusta, ma non sono convinto del resto.
    infatti facendo il debug con l'interprete php mi solleva un warning.
    Potete guardare il codice che sinceramente non so da dove partire.

    grazie
    L'importante no è quello che trovi alla fine di una corsa.
    L'importante è quello che provi mentre corri.
    ----

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.