1

I am trying to get elements by tag name from this xml file.

<cogxml>
    <namespace name="http://www.lirmm.fr/cogui#" prefix=""/>
    <support name="vocabulary">
       <conceptTypes>
           <ctype id="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" label="Junior Employee" x="250" y="10">
               <translation descr="" label="Junior Employee" lang="en"/>
           </ctype>
           <ctype id="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" label="Employee" x="130" y="60">
               <translation descr="" label="Employee" lang="en"/>
           </ctype>
           <ctype id="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" label="Director" x="250" y="110">
               <translation descr="" label="Director" lang="en"/>
           </ctype>
           <ctype id="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" label="Manager" x="250" y="60">
               <translation descr="" label="Manager" lang="en"/>
           </ctype>
           <ctype id="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" label="Senior Employee" x="255" y="190">
               <translation descr="" label="Senior Employee" lang="en"/>
           </ctype>
           <ctype id="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="Top" x="10" y="60">
               <translation descr="" label="Top" lang="en"/>
           </ctype>
           <order id1="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/>
           <order id1="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/>
           <order id1="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" id2="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288"/>
           <order id1="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/>
           <order id1="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" id2="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00"/>
       </conceptTypes>
       <relationTypes>
           <rtype id="http://www.lirmm.fr/cogui#_rt_c42a5ce6-2f20-491d-8c91-501ae178a36c" idSignature="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288 http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="Link" x="10.0" y="10.0">
              <translation descr="" label="Link" lang="en"/>
           </rtype>
           <rtype id="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" idSignature="http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288 http://www.lirmm.fr/cogui#_ct_a12bacc5-bc88-429e-a7b1-45e143591288" label="senior" x="70.0" y="10.0">
              <translation descr="" label="senior" lang="en"/>
           </rtype>
           <order id1="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" id2="http://www.lirmm.fr/cogui#_rt_c42a5ce6-2f20-491d-8c91-501ae178a36c"/>
      </relationTypes>
      <nestingTypes>
          <ntype id="http://www.lirmm.fr/cogui#_nt_4d626655-02b5-496e-b19c-f4cdb72ee70a" label="Nesting">
              <translation descr="" label="Nesting" lang="en"/>
          </ntype>
      </nestingTypes>
      <conformity>
          <marker id="http://www.lirmm.fr/cogui#i_435d513c-1b39-43b1-9b6d-310fa0ee46d3" idType="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" label="Lucy"/>
          <marker id="http://www.lirmm.fr/cogui#i_80311575-7d72-4af7-bdbe-a19c4bbcf248" idType="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" label="Simon"/>
          <marker id="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" label="Robyn"/>
          <marker id="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" label="Richard"/>
      </conformity>
      <modules/>
   </support>
   <localeTypes name="undefined_vocabulary">
      <conceptTypes/>
      <relationTypes/>
      <nestingTypes/>
      <conformity/>
      <modules/>
   </localeTypes>
   <graph id="_g1" label="seniorities" nature="fact" set="default_set">
      <concept id="c_f55e1936-7842-4518-b460-bb34a9000871" idMarker="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" referent="individual" x="0" y="0"/>
      <concept id="c_f081c276-57ff-4650-94ec-6e40dfd38023" idMarker="http://www.lirmm.fr/cogui#i_80311575-7d72-4af7-bdbe-a19c4bbcf248" idType="http://www.lirmm.fr/cogui#ct_cd84c648-ef22-4854-8e8c-a6654c0386be" referent="individual" x="185" y="70"/>
      <concept id="c_f0229ed1-c913-4373-af9c-361a90a56e90" idMarker="http://www.lirmm.fr/cogui#i_435d513c-1b39-43b1-9b6d-310fa0ee46d3" idType="http://www.lirmm.fr/cogui#ct_710bed80-a33e-4a13-b916-15fbb3357e8d" referent="individual" x="330" y="170"/>
      <concept id="c_dbe5b7cb-7d00-44f1-8b9a-832d5b61a126" idMarker="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" referent="individual" x="445" y="260"/>
      <relation id="r_10008dd3-5426-4c87-8651-049045f98376" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="90" y="70"/>
      <relation id="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="235" y="135"/>
      <relation id="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="355" y="260"/>
      <edge cid="c_f55e1936-7842-4518-b460-bb34a9000871" label="1" rid="r_10008dd3-5426-4c87-8651-049045f98376"/>
      <edge cid="c_f081c276-57ff-4650-94ec-6e40dfd38023" label="2" rid="r_10008dd3-5426-4c87-8651-049045f98376"/>
      <edge cid="c_f081c276-57ff-4650-94ec-6e40dfd38023" label="1" rid="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a"/>
      <edge cid="c_f0229ed1-c913-4373-af9c-361a90a56e90" label="2" rid="r_8ef0b4bf-3cb6-4dde-9c83-903cb459872a"/>
      <edge cid="c_f0229ed1-c913-4373-af9c-361a90a56e90" label="1" rid="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1"/>
      <edge cid="c_dbe5b7cb-7d00-44f1-8b9a-832d5b61a126" label="2" rid="r_563d1804-04eb-45d4-81c9-f4bd4782e0b1"/>
   </graph>
   <graph id="_query1" label="Richard senior to Robyn" nature="query" set="default_set">
      <concept id="c_90dc1159-1d02-4707-a444-2e95817d8667" idMarker="http://www.lirmm.fr/cogui#i_d1a8babc-3d35-4580-b4d5-d3cd4c323c98" idType="http://www.lirmm.fr/cogui#ct_043ea910-5f86-4150-b0f1-1418acf4db39" referent="individual" x="30" y="165"/>
      <concept id="c_4af4cf1c-5383-413b-bee2-7a4c513fd37e" idMarker="http://www.lirmm.fr/cogui#i_efbef15b-d6e2-4c0a-8155-ecaae75cc673" idType="http://www.lirmm.fr/cogui#ct_feeca670-2f1c-433e-9271-4cffeda1e929" referent="individual" x="45" y="25"/>
      <relation id="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="75" y="95"/>
     <edge cid="c_90dc1159-1d02-4707-a444-2e95817d8667" label="1" rid="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78"/>
     <edge cid="c_4af4cf1c-5383-413b-bee2-7a4c513fd37e" label="2" rid="r_c51c5a9c-3cf5-4402-a356-03c9882f6b78"/>
   </graph>
   <rule id="_rule1">
      <hypt>
         <graph id="_rule1_hypt" label="seniority rule" nature="rule" set="default_set">
         <concept id="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="45"/>
         <concept id="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="173"/>
         <concept id="c_669f89c9-eb93-4caa-aa90-f8e31be92245" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="30" y="293"/>
         <relation id="r_93ecba23-873d-490c-8ce3-40611158006b" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="45" y="113"/>
         <relation id="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="53" y="233"/>
         <edge cid="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" label="1" rid="r_93ecba23-873d-490c-8ce3-40611158006b"/>
         <edge cid="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" label="2" rid="r_93ecba23-873d-490c-8ce3-40611158006b"/>
         <edge cid="c_f765dff9-1cd4-42f1-bf95-87cbda00257d" label="1" rid="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52"/>
         <edge cid="c_669f89c9-eb93-4caa-aa90-f8e31be92245" label="2" rid="r_257f3815-2ac4-4b9e-8b8c-7d9ae6259b52"/>
      </graph>
    </hypt>
    <conc>
       <graph id="_rule1_conc">
           <concept id="c_fe469224-c26a-49dc-a17a-697faa20aca3" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="173" y="53"/>
           <concept id="c_656473af-5b41-4ed7-b4fc-be3af9ce544d" idType="http://www.lirmm.fr/cogui#ct_d7a78641-722f-4609-8f5a-90affc111e00" x="180" y="293"/>
           <relation id="r_3b36e657-bc62-4050-baad-c3a04ade3af2" idType="http://www.lirmm.fr/cogui#rt_af40394c-9e62-4e92-b05b-352de5db876f" x="203" y="158"/>
           <edge cid="c_fe469224-c26a-49dc-a17a-697faa20aca3" label="1" rid="r_3b36e657-bc62-4050-baad-c3a04ade3af2"/>
           <edge cid="c_656473af-5b41-4ed7-b4fc-be3af9ce544d" label="2" rid="r_3b36e657-bc62-4050-baad-c3a04ade3af2"/>
        </graph>
      </conc>
      <conPts>
         <couple idC1="c_591883b6-ca82-42ee-bd35-b4ce29ffd286" idC2="c_fe469224-c26a-49dc-a17a-697faa20aca3"/>
         <couple idC1="c_669f89c9-eb93-4caa-aa90-f8e31be92245" idC2="c_656473af-5b41-4ed7-b4fc-be3af9ce544d"/>
      </conPts>
   </rule>
</cogxml>

I need to output it like this for each ctype. The order id1 and rtype ID has to match the ctype ID.

   <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <cogxml>
        <support name="vocabulary">
           <conceptTypes>
               <ctype id="http://www.lirmm.fr/cogui#ct_ef8dc30a-fc37-48fa-80f3-4588e4d101d2" label="Adult" x="70" y="60">
                  <translation descr="" label="Adult" lang="en"/>
               </ctype>
               <order id1="http://www.lirmm.fr/cogui#ct_ef8dc30a-fc37-48fa-80f3-4588e4d101d2" id2="http://www.lirmm.fr/cogui#_ct_5f3287f9-fb7b-47c2-84b0-e25694b29841"/>
           </conceptTypes>
           <relationTypes>
                <rtype id="http://www.lirmm.fr/cogui#rt_05fd573c-12e2-468e-aee9-746ef400e8d1" idSignature="http://www.lirmm.fr/cogui#ct_ef8dc30a-fc37-48fa-80f3-4588e4d101d2 http://www.lirmm.fr/cogui#_ct_5f3287f9-fb7b-47c2-84b0-e25694b29841" label="Parent Of Child">
                   <translation descr="" label="Parent Of Child" lang="en"/>
                </rtype>
           </relationTypes>
       </support>
    </cogxml>

How would I match the ctype id with the rtype id and order id Dynamically? I've tried to do it using the link in the second answer but I don't know how you would do it and how would I put that small section of code on a new row in the table? At the moment I have got this PHP code but I don't know how to do the if statement at the bottom so that in the table, something like the section of code above appears in each row depending on the conceptTypes.

<?php 

$dom = new DOMDocument('1.0', 'utf-8');
                                $dom->formatOutput = true;
$dom->preserveWhiteSpace = false;

$dom->load($name);

$xpath = new DOMXPath($dom);

foreach ($xpath->query("/cogxml/graph")
 as $node) {

    $node->parentNode->removeChild($node); 

 }
 foreach ($xpath->query("/cogxml/rule")
 as $node1) {

    $node1->parentNode->removeChild($node1); 

 }

 foreach ($xpath->query("/cogxml/namespace")
 as $node2) {

    $node2->parentNode->removeChild($node2); 

 }

 foreach ($xpath->query("/cogxml/support/nestingTypes")
 as $node3) {

    $node3->parentNode->removeChild($node3); 

 }

 foreach ($xpath->query("/cogxml/support/modules")
 as $node4) {

    $node4->parentNode->removeChild($node4); 

 }

  foreach ($xpath->query("/cogxml/support/conformity")
 as $node5) {

    $node5->parentNode->removeChild($node5); 

 }

 foreach ($xpath->query("/cogxml/localeTypes")
 as $node6) {

    $node6->parentNode->removeChild($node6); 

 }

 foreach ($xpath->query("/cogxml/support/relationTypes/order")
 as $node7) {

    $node7->parentNode->removeChild($node7); 

 }

 foreach ($xpath->query("/cogxml/support/bannedTypes")
 as $node8) {

    $node8->parentNode->removeChild($node8); 

 }

 //$ctypeid=$dom->suppprt->conceptTypes->ctype['id'];
 foreach ($xpath->query("/cogxml/support/conceptTypes/ctype") as $node9){
 if ($xpath->query("/cogxml/support/conceptTypes/ctype['id']") != $xpath->query("/cogxml/support/relationTypes/rtype['idsignature']") && $xpath->query("/cogxml/support/conceptTypes/order['id1']")) {
  #if($ctype[0] == $xpath->query(){
    $node9->parentNode->removeChild($node9);      
  }

}

#if($dom->cogxml->support->conceptTypes->ctype['id'] )

 #$xml->formatOutput = true;
 $dom->save('test.xml');
 ?>
 <pre><code class="language-xml"><?php echo htmlspecialchars(file_get_contents('test.xml'), ENT_QUOTES); ?></code></pre>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Charlotte
  • 37
  • 10
  • can't see any of this matching in the code you posted? Is this code that you wrote to perform this task? – michi Feb 12 '16 at 16:46
  • Yes it is but the above code only removes the different concept types it doesn't remove the localeTypes and graph etc. It's only supposed to display the ctype and which order and Relation Types ids match the ctype id. Is there an easier way to do this using simple xml or xpath? – Charlotte Feb 12 '16 at 16:51
  • Your question is still unclear to me. You say "For each `conceptType` the `order id1` and `relationType ID` has to match." - but in your example of the desired result, they are different (and even named differently: `rtype`, BTW). I suggest you start by editing your question and building a **minimal SIMPLIFIED valid example** of that XML. Those ids drive me nuts :-) – michi Feb 12 '16 at 18:17

2 Answers2

1

localeTypes and graph elements are separated from support element's content.
They are placed on the same level as support element(not inside $xml->support->localeTypes).
To remove the intended elements change your code as shown below:

...
foreach ($xml->localeTypes as $code6){
    $elementsToRemove[] = $code6;
}
foreach ($xml->graph as $code){
    $elementsToRemove[] = $code;
}
...
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Ok thank you. yes i've just realised this. I've got this working now. Do you know how i can loop through the `ctypes` `relation types` and `order` to display the `ids` that match? – Charlotte Feb 12 '16 at 18:10
  • @Charlotte, do you want to display all `order` and `relation types` elements which `id` attribute has the similar equivalent within `ctypes` elements ids ? – RomanPerekhrest Feb 12 '16 at 18:47
  • Yes thats exactly Wat I want to do but I'm not sure how to go about it. – Charlotte Feb 12 '16 at 20:16
0

As a general strategy, you seem to delete all non-matching nodes from the XML.

As a next step, you should iterate over <ctype> and get its id attribute:

foreach ($xml->support-conceptTypes->ctype as $ctype) {
    $ctypeid = (string)$ctype['id'];
}

Then, you can iterate over all <order> and then all <rtype> nodes and delete those that do not match $ctypeid.

Alternative #1 is to use xpath instead of iterating and comparing.

Alternative #2 is - instead of deleting nodes you do not need - to select nodes you do need and build a new XML from there.

I suggest you write some code, test it, and update your question if a new problem arises.

Again, for testing purposes, I strongly suggest to build a simpler XML by replacing the complex links in the id attribute by a simple number or word.

michi
  • 6,565
  • 4
  • 33
  • 56
  • Thanks this is exactly what I want to do. How would I iterate over and get Id and and nodes with xpath? I tried it but I couldn't get it to work. – Charlotte Feb 12 '16 at 20:14
  • @Charlotte: see my updates. xpath: search StackOverflow, many great examples here, e.g. http://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value – michi Feb 12 '16 at 20:17