循环遍历xml元素并在新行的表中显示? [英] Loop through xml elements and display in a table in a new row?
问题描述
更新:
如何获取此代码以显示在表的每一行中。到目前为止,我已经有了该表,并且我尝试将这段代码放在< td>
之前和之内,但不会将上面的xml文件放在新的行上每个< cogxml>
。可以看到我要去哪里了吗?测试2.php是Parfait在回答中提供的php代码。
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>
这就是现在的样子。每一行看起来都是这样,但我只希望cogxml的每一部分都出现在每一行中。
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.
推荐答案
如上所述,请考虑使用XSLT解决方案。具体来说,您需要使用<$ c $上的 Muenchian分组的XSLT。 c> ctype / @ id 值。 PHP可以处理在单独文件中外部调用或作为字符串嵌入的XSLT。下面运行前者:
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 脚本(另存为.xsl或.xslt,将在下面的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 脚本
// 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);
转换后的XML (每个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>
要呈现HTML表,您可以扩展XSLT上面的脚本并保留确切的PHP脚本,只是将输出另存为HTML: Output.html。回想一下XSLT将XML转换为多种格式:XML,HTML甚至文本。此处< cogxml>
将在表的每一行中呈现。
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 表:
<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>
这篇关于循环遍历xml元素并在新行的表中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!