Charlotte Charlotte - 2 months ago 5
PHP Question

Loop through xml elements and display in a table in a new row?

UPDATE:

How would I get this code to display in each row in a table. I have this table so far and I've tried putting this code before and inside the

<td>
but it won't put the above xml file onto a new line for each
<cogxml>
. Can you see where I am going wrong? Test 2.php is the php code that Parfait has in the answer.

<?php
include_once('test2.php');
?>


<html>

<head>
<title>CharGer and CoGui</title>
<link rel="stylesheet" type="text/css" href="../XSLT/myStyle.css" />
</head>

<body>
<div style="overflow-x:auto;">
<?php
include_once("../XSLT/upload4.php");
?>
<div class="wrap">
<table>
<tr>
<th width="5%">Concept Name</th>
<th width="5%">Relation Type</th>
<th width="44%">CoGui XML</th>
<th width="46%">CharGer XML</th>
</tr>
<tr>
<?php
for ($x=0; $x <=5; $x++){?>
<td>

</td>
<td>

</td>
<td>

<pre><code class="language-xml"><?php echo htmlspecialchars(file_get_contents($xmlfile), ENT_QUOTES); ?></code></pre>


</td>
<td>
</td>

</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>


This is what it looks like at the moment. It looks like this on each line but I only want each section of cogxml to appear in each row.
enter image description here

Answer

As mentioned, consider an XSLT solution. Specifically, you will want an XSLT using the Muenchian Grouping on the ctype/@id value. PHP can process XSLT called externally in a separate file or embedded as a string. Below runs the former:

XSLT Script (save as .xsl or .xslt to be used below in PHP)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:key name="idkey" match="ctype" use="@id" />

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="*"/>
    </root>
  </xsl:template>

  <xsl:template match="ctype[generate-id() = generate-id(key('idkey',@id)[1])]">
    <xsl:variable select="@id" name="id"/>
    <cogxml>
      <support>
        <xsl:attribute name="name"><xsl:value-of select="ancestor::support/@name"/></xsl:attribute>            
        <conceptTypes>                    
          <xsl:copy>
            <xsl:copy-of select="@*"/>          
            <xsl:copy-of select="translation"/>
          </xsl:copy>
          <xsl:copy-of select="ancestor::conceptTypes/order[@id1=$id]"/>        
        </conceptTypes>      
        <relationTypes>
            <xsl:copy-of select="ancestor::support/relationTypes/rtype[contains(@idSignature, $id)]"/>
         </relationTypes>
      </support>
    </cogxml>
  </xsl:template>

</xsl:transform>

PHP Script

// Load the XML source and XSLT file
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('Input.xml');

$xsl = new DOMDocument;
$xsl->load('XSLTScript.xsl');

// Configure transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

// Transform XML source
$newXML = new DOMDocument;
$newXML = $proc->transformToXML($xml);
echo $newXML;

// Save output to file
$xmlfile = 'Output.xml';
file_put_contents($xmlfile, $newXML);

Transformed XML (separated by each cType)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <cogxml>
    <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>
        <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"/>
      </conceptTypes>
      <relationTypes/>
    </support>
  </cogxml>
  <cogxml>
    <support name="vocabulary">
      <conceptTypes>
        <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>
        <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"/>
      </conceptTypes>
      <relationTypes/>
    </support>
  </cogxml>
  <cogxml>
    <support name="vocabulary">
      <conceptTypes>
        <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>
        <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"/>
      </conceptTypes>
      <relationTypes/>
    </support>
  </cogxml>
  <cogxml>
    <support name="vocabulary">
      <conceptTypes>
        <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>
        <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/>
    </support>
  </cogxml>
  <cogxml>
    <support name="vocabulary">
      <conceptTypes>
        <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>
        <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"/>
      </conceptTypes>
      <relationTypes/>
    </support>
  </cogxml>
  <cogxml>
    <support name="vocabulary">
      <conceptTypes>
        <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>
      </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>
      </relationTypes>
    </support>
  </cogxml>
</root>

To render HTML table, you can extend the XSLT script above and keep exact PHP script but just save output as HTML: 'Output.html'. Recall XSLT transforms XML into multiple formats: XML, HTML, even text. Here <cogxml> will render in each row of table.

HTML Table:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:key name="idkey" match="ctype" use="@id" />

  <xsl:template match="/">
     <html>

      <head>
        <title>CharGer and CoGui</title>
        <link rel="stylesheet" type="text/css" href="../XSLT/myStyle.css" />
      </head>

      <body>
        <div style="overflow-x:auto;">
          <div class="wrap">
              <table>
                    <tr>
                      <th width="5%">Concept Name</th>
                      <th width="5%">Relation Type</th>
                      <th width="44%">CoGui XML</th>
                      <th width="46%">CharGer XML</th>
                    </tr>
                      <xsl:apply-templates select="*"/>
              </table>
          </div>
        </div>
      </body>
     </html>

  </xsl:template>

  <xsl:template match="ctype[generate-id() = generate-id(key('idkey',@id)[1])]">
    <xsl:variable select="@id" name="id"/>
      <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
          <pre>

          <cogxml>
            <support>
              <xsl:attribute name="name"><xsl:value-of select="ancestor::support/@name"/></xsl:attribute>            
              <conceptTypes>                    
                <xsl:copy>
                  <xsl:copy-of select="@*"/>          
                  <xsl:copy-of select="translation"/>
                </xsl:copy>
                <xsl:copy-of select="ancestor::conceptTypes/order[@id1=$id]"/>        
              </conceptTypes>      
              <relationTypes>
                  <xsl:copy-of select="ancestor::support/relationTypes/rtype[contains(@idSignature, $id)]"/>
               </relationTypes>
            </support>
          </cogxml>


          </pre>
        </td>
      </tr> 
  </xsl:template>

</xsl:transform>