问题
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.
回答1:
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>
回答2:
Instead of deleting the non-matches, build a new file for each <ctype>
.
To accomplish this,
1. iterate over all <ctype>
nodes
1. get the id
-attribute and select all <order>
and <rtype>
nodes that match
2. copy the results to a new DOM.
Iterate over all <ctype>
nodes, get the id:
foreach ($dom->getElementsByTagName("ctype") as $ctype) {
$id = $ctype->getAttribute('id');
// more code will go here...
}
select the matches, using xpath
Set up the xpath
-expressions:
/cogxml/support/conceptTypes/order[@id1='$id']
Note the condition is in []
, the @
signals attribute
. In this case, id1
-attribute of the <order>
node has to match $id
:
$xpath = new DOMXPath($dom);
$orders = $xpath->query("/cogxml/support/conceptTypes/order[@id1='$id']");
Result is a DOMNodeList
as $orders
, there might be 0
or more matches.
Same procedure matching the id
-attribute of the <rtype>
-nodes.
build a new DOM and copy the nodes from the source to it
$newdom = new DOMDocument('1.0', 'utf-8');
$newdom->loadXML("<cogxml><support><conceptTypes /><relationTypes /></support></cogxml>");
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 call appendChild
:
$newnode = $newdom->importNode($ctype, true);
$newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode);
There might be multiple matching orders, so we iterate:
foreach ($orders as $order) {
$newnode = $newdom->importNode($order, true);
$newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode);
}
Copying $rtypes
is following the same procedure.
Finally, save the new DOM to XML:
$newdom->saveXML("mynewfile.xml");
Putting it all together and streamlining:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($x); // assume source XML in $x
$newdom = new DOMDocument('1.0', 'utf-8');
$xpath = new DOMXPath($dom);
foreach ($dom->getElementsByTagName("ctype") as $ctype) {
$newdom->loadXML("<cogxml><support><conceptTypes /><relationTypes /></support></cogxml>");
$newnode = $newdom->importNode($ctype, true);
$newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode);
$id = $ctype->getAttribute('id');
foreach ($xpath->query("/cogxml/support/conceptTypes/order[@id1='$id']") as $order) {
$newnode = $newdom->importNode($order, true);
$newdom->getElementsByTagName("conceptTypes")->item(0)->appendChild($newnode);
}
foreach ($xpath->query("/cogxml/support/relationTypes/rtype[@id='$id']") as $rtype) {
$newnode = $newdom->importNode($rtype, true);
$newdom->getElementsByTagName("relationTypes")->item(0)->appendChild($newnode);
}
echo $newdom->saveXML(); // echo to screen
}
see it in action: https://eval.in/527124
来源:https://stackoverflow.com/questions/35684886/loop-through-xml-elements-and-display-in-a-table-in-a-new-row