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.
Instead of deleting the non-matches, build a new file for each
<ctype>
.To accomplish this,
1. iterate over all
<ctype>
nodes1. get the
id
-attribute and select all<order>
and<rtype>
nodes that match2. copy the results to a new DOM.
Iterate over all
<ctype>
nodes, get the id:select the matches, using
xpath
Set up the
xpath
-expressions:Note the condition is in
[]
, the@
signalsattribute
. In this case,id1
-attribute of the<order>
node has to match$id
:Result is a
DOMNodeList
as$orders
, there might be0
or more matches. Same procedure matching theid
-attribute of the<rtype>
-nodes.build a new DOM and copy the nodes from the source to it
This is setting up the basic XML corpus. Now add the
<ctype>
and its matching nodes we selected -importNode()
will do the job. Note that we need the parent<conceptTypes>
in the new DOM to callappendChild
:There might be multiple matching orders, so we iterate:
Copying
$rtypes
is following the same procedure.Finally, save the new DOM to XML:
Putting it all together and streamlining:
see it in action: https://eval.in/527124
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)
PHP Script
Transformed XML (separated by each cType)
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: