-1

Any idea how can join and ID with another set of data and then print out certain information? I want to print out The name and the country of a user. I am stuck already for a few hours and I would appreciate any help and suggestion!

These are the the files : https://drive.google.com/drive/folders/15vi1sW5TExqZBCAz-gUTWF5xYCq9pC4u?usp=sharing

EDIT: The code that I wrote is the problem. I get partial results but I am missing the "JOIN" between different XML sets of data.

Edit2: I need to obtain as a result: 1) A project title 2) Acronym 3) Abstract and 4) Partner name and country. I am missing part 4) where I was trying to join based on PartnerRef values and by the list of partners at the end of the xml file.

**I try to achieve this: ** enter image description here

XML:

<?xml version="1.0" encoding="UTF-8"?>


<InfoSystem>
    <Workflows>
        <Workflow>
            <Name>project-workflow</Name>
            <Transition>
                <From>in-preparation</From>
                <To>submitted</To>
            </Transition>
            <Transition>
                <From>submitted</From>
                <To>accepted</To>
                <To>rejected</To>
            </Transition>
            <Transition>
                <From>accepted</From>
                <To>running</To>
            </Transition>
            <Transition>
                <From>running</From>
                <To>completed</To>
            </Transition>
        </Workflow>
    </Workflows>
    <Projects>
        
        
        <Project>
            <Id>3</Id>
            <StatusHistory>
                <Status>
                    <Value>in-preparation</Value>
                    <Date>2019-01-20</Date>
                </Status>
                <Status>
                    <Value>submitted</Value>
                    <Date>2019-03-20</Date>
                </Status>
                <Status>
                    <Value>rejected</Value>
                    <Date>2019-06-01</Date>
                </Status>
            </StatusHistory>
            <Submission>
                <Title>Secure environments for e-commerce</Title>
                <Acronym>SEEC</Acronym>
                <Abstract>
                    <Parag><Fragment>The project aims at providing innovative solutions for secure e-commerce</Fragment></Parag>
                </Abstract>
                
                <WorkPackages>
                    <WorkPackage>
                        <No>1</No>
                        <Title>Coordination</Title>
                        <Tasks>
                            <Task>
                                <No>1</No>
                                <Name>Setting-up the project framework</Name>
                                <Start>2019-07-01</Start>
                                <End>2019-09-01</End>
                                <Effort>
                                    <Partner>
                                        <PartnerRef>4</PartnerRef>
                                        <DaysNb>20</DaysNb>
                                    </Partner>
                                </Effort>
                            </Task>
                            <Task>
                                <No>2</No>
                                <Name>Project management</Name>
                                <Start>2019-07-01</Start>
                                <End>2019-07-01</End>
                                <Effort>
                                    <Partner>
                                        <PartnerRef>4</PartnerRef>
                                        <DaysNb>100</DaysNb>
                                    </Partner>
                                    <Partner>
                                        <PartnerRef>6</PartnerRef>
                                        <DaysNb>40</DaysNb>
                                    </Partner>
                                    <Partner>
                                        <PartnerRef>7</PartnerRef>
                                        <DaysNb>40</DaysNb>
                                    </Partner>
                                </Effort>
                            </Task>
                        </Tasks>
                        <Deliverables>
                            <Deliverable>
                                <Id>1</Id>
                                <Name>Management report 1</Name>
                                <Type>report</Type>
                                <DueDate>2019-12-20</DueDate>
                            </Deliverable>
                            <Deliverable>
                                <Id>2</Id>
                                <Name>Management report 2</Name>
                                <Type>report</Type>
                                <DueDate>2020-06-30</DueDate>
                            </Deliverable>
                            <Deliverable>
                                <Id>3</Id>
                                <Name>Final management report</Name>
                                <Type>report</Type>
                                <DueDate>2021-07-01</DueDate>
                            </Deliverable>
                        </Deliverables>
                    </WorkPackage>
                    <WorkPackage>
                        <No>2</No>
                        <Title>Design of the platform</Title>
                        <Tasks>
                            <Task>
                                <No>1</No>
                                <Name>User interface design</Name>
                                <Start>2020-09-01</Start>
                                <End>2020-12-12</End>
                                <Effort>
                                    <Partner>
                                        <PartnerRef>6</PartnerRef>
                                        <DaysNb>150</DaysNb>
                                    </Partner>
                                    <Partner>
                                        <PartnerRef>7</PartnerRef>
                                        <DaysNb>220</DaysNb>
                                    </Partner>
                                </Effort>
                            </Task>
                        </Tasks>
                        <Deliverables>
                            <Deliverable>
                                <Id>1</Id>
                                <Name>Paper about existing approaches</Name>
                                <Type>publication</Type>
                                <DueDate>2020-10-30</DueDate>
                            </Deliverable>
                        </Deliverables>
                    </WorkPackage>
                </WorkPackages>
            </Submission>
        </Project>
        <Project>
            <Id>4</Id>
            <StatusHistory>
                <Status>
                    <Value>in-preparation</Value>
                    <Date>2022-02-02</Date>
                </Status>
            </StatusHistory>
            <Submission>
                <Title>An approach to develop Web-based applications</Title>
                <Acronym>WBA</Acronym>
                <Abstract><Parag><Fragment>The approach relies on the use of semi-structured data</Fragment></Parag></Abstract>
            </Submission>
        </Project>
    </Projects>
    <Partners>
        <Partner>
            <Id>1</Id>
            <Name>UNI1</Name>
            <Country>CH</Country>
        </Partner>
        <Partner>
            <Id>2</Id>
            <Name>UNI2</Name>
            <Country>FR</Country>
        </Partner>
        <Partner>
            <Id>3</Id>
            <Name>Pear</Name>
            <Country>BE</Country>
        </Partner>
        <Partner>
            <Id>4</Id>
            <Name>CIMER</Name>
            <Country>FR</Country>
        </Partner>
        <Partner>
            <Id>5</Id>
            <Name>LFPE</Name>
            <Country>CH</Country>
        </Partner>
        <Partner>
            <Id>6</Id>
            <Name>UNI3</Name>
            <Country>IT</Country>
        </Partner>
        <Partner>
            <Id>7</Id>
            <Name>UNI4</Name>
            <Country>BE</Country>
        </Partner>
        <Partner>
            <Id>8</Id>
            <Name>APER</Name>
            <Country>GR</Country>
        </Partner>
    </Partners>
    <Publications>
        <PaperInJournal>
            <Id>1</Id>
            <Title>Principles of remote learning</Title>
            <Authors>
                <Author>
                    <FirstName>Alain</FirstName>
                    <LastName>Térieur</LastName>
                </Author>
                <Author>
                    <FirstName>Alex</FirstName>
                    <LastName>Térieur</LastName>
                </Author>
            </Authors>
            <Year>2022</Year>
            <Journal>Journal of e-learning</Journal>
            <Vol>3</Vol>
            <No>11</No>
            <Pages>
                <From>34</From>
                <To>45</To>
            </Pages>
        </PaperInJournal>
        <PaperInJournal>
            <Id>2</Id>
            <Title>Authoring multimedia</Title>
            <Authors>
                <Author>
                    <FirstName>Anne</FirstName>
                    <LastName>Dupont</LastName>
                </Author>
                <Author>
                    <FirstName>Victor</FirstName>
                    <LastName>Térieur</LastName>
                </Author>
            </Authors>
            <Year>2021</Year>
            <Journal>Information systems</Journal>
            <Vol>5</Vol>
            <No>3</No>
            <Pages>
                <From>134</From>
                <To>145</To>
            </Pages>
        </PaperInJournal>
    </Publications>
</InfoSystem>

php code:

    <?php
    include 'info-systems.php';
    include 'example.php';
    
    $InfoSystem = simplexml_load_file("info-systems.xml");
    
    foreach($InfoSystem->xpath('//Project') as $project){
        $projectId = $project->Id;
        $selectedPartner = $project->xpath('//PartnerRef');
        //print_r($selectedPartner);
        echo "Title: " . $project->Submission->Title;
        echo "<br>";
        echo "Acronym: " . $project->Submission->Acronym;
        echo "<br>";
        echo "Abstract: " . $project->Submission->Abstract->Parag->Fragment;
        echo "<br>";
        echo $project->Submission->Abstract->List->asXML();
        echo "<br>";
    
        foreach($InfoSystem->xpath('//Partner') as $partner){
    
                if(in_array(1, $selectedPartner, TRUE)){ // This doesn't work at all
                    echo 'Name: ' . $partner->Name;
                    echo 'Country: ' . $partner->Country;
                }
    
            //echo $partner->Name;
           
        }
    
        echo "<br><br>";
    }

?>
  • 3
    I'm not clear what we're looking at here - is that code that you've attempted the problem but not for the result you wanted? What result did you get instead? Is all that code definitely relevant, including the parts referencing XML elements you haven't shown us? Please can you [edit] the question to contain a [mre] and a clear description of what it does. – IMSoP May 22 '22 at 09:48
  • @IMSoP Thank you for all your suggestions, I edited the answer and uploaded the code folder, thank you for your very quick reply to my challenge and for trying to help me out! – Alexandru Filipescu May 22 '22 at 10:06
  • 4
    Sorry, I'm not going to download an entire folder of files just to understand your question; it's up to you to provide a [mre] so people can see what you're trying to do and help you. – IMSoP May 22 '22 at 10:08
  • Perhaps the answers here are what you are looking for? https://stackoverflow.com/q/992450/157957 – IMSoP May 22 '22 at 10:10
  • I would start by loading all the partners into an array. – Olivier May 22 '22 at 10:17
  • @IMSoP I re-edited the post, I hope that it is more clear now! The problem with your approach is that for the moment I don't have any attribute on any element! Thank you – Alexandru Filipescu May 22 '22 at 10:41
  • 1
    First, your xml is not well formed, so please fix it. Second, it's not clear if one file can have more than one `` or if there can be more than one in a file. If more than one, please show how the `` are associated with each ``. – Jack Fleeting May 22 '22 at 13:31
  • @JackFleeting I pasted the full XML in a snippet, the file can have more than one `. The Project partners have multiple tags which contain a that holds a number. I want to connect that number from PartnerRef to the tags found in the list of – Alexandru Filipescu May 22 '22 at 16:39

1 Answers1

0

If I understand you correctly, you are probably looking for something like the following (or close to - you can modify it to fit your task). Since your xml is deeply nested, you need to nest your code as well.... We'll use xpath to get there:

#locate each project
$projects = $InfoSystem->xpath("//Project");

foreach ($projects as $project) {
    #get project title and abstract
    $title = trim($project->xpath('.//Submission/Title')[0]);
    $abst = trim($project->xpath('.//Abstract//Fragment/text()')[0]);
    echo "Project Title: " . $title."\n\r";
    echo "Abstract: ". $abst."\n\r";;

    #project are (sometimes) divided into work packages; locate these: 
    $wps = $project->xpath(".//WorkPackage");
    foreach ($wps as $wp) {
      #get the work package title
      $wptitle = $wp->xpath('.//Title')[0];
      echo "Work Package Title: ".$wptitle."\r\n";

      #work packages are (sometimes) divided into tasks; get these as well
      #and so on for other sub-components of the project
      $tasks = $wp->xpath('.//Task');
      foreach ($tasks as $task) {
          $taskname= $task->xpath('.//Name')[0];
          echo "Task Name: ". $taskname."\r\n";
          $partners = $task->xpath('.//PartnerRef');
          echo "Task Partners: "."\n\r";
          foreach ($partners as $partner) {
            $exp = "//Partners//Partner[./Id=($partner)]";
            $tparts = $InfoSystem->xpath($exp);
            foreach ($tparts as $tp) {
              $tpname = $tp->xpath('.//Name')[0];
              $tpcountry = $tp->xpath('.//Country')[0];
              echo "Partner Name: ". $tpname." Country: ". $tpcountry."\r\n";
              }
         }
    }
    }
    echo "--------end project-------\r\n";
   
    }

And this should get you close enough.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45