我有一个PHP脚本,它可以读取XML文件并将其转换为我的XML格式。原始 XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<root>
<products>
<product>
<id>1</id>
<stock>23</stock>
... 6 more nodes....
<pictures>
<pic1>linktopic1.jpg</pic1>
<pic2>linktopic2.jpg</pic2>
</pictures>
<tax_percent>...</tax_percent>
<price_dc>....</price_dc>
...... some more nodes....
<attributes>
<attribute name="Sample attribute" value="Sample value" />
<attribute name="Sample attribute 2" value="Sample value 2"/>
.... 10+ attributes for each product....
</attributes>
</product>
</products>
</root>
我正在使用XMLwriter来读取XML并写入新的XML。位于单独节点中的所有图片和属性必须一起放在 1 个字符串中。我设法为图片执行此操作,但它不适用于属性。
foreach($xml->children() as $products) {
foreach($products->children() as $product) {
$newXML->startElement("product");
$newXML->writeElement("ID", $product->id);
$newXML->writeElement("Stock", $product->stock);
$pic=""; // string resets for each product node
$atr=""; // Attribute string
foreach($product->pictures as $child) { // pictures
if ($child->pic1!="") { $pic=$pic.$child->pic1.","; } // If the picture subnode exists it appends the image to previous ones
if ($child->pic2!="") { $pic=$pic.$child->pic2.","; }
if ($child->pic3!="") { $pic=$pic.$child->pic3.","; }
}
foreach($product->attributes as $child) { //attributes
if ($child->attribute['name']=="Sample attribute") { $atr=$atr.$child->attribute['name'].':'.$child->attribute['value'].','; }
if ($child->attribute['name']=="Sample attribute 2") { $atr=$atr.$child->attribute['name'].':'.$child->attribute['value'].','; }
}
..... some more spaghetti code....
就像我说的,图片的代码正在工作,但由于某种原因,属性代码只在字符串中写入产品的第一个属性,并跳过所有其他属性。
有人知道为什么 foreach 循环会跳过所有其他属性节点吗?对我来说,它似乎有某种问题,因为属性节点具有相同的节点名称,而图片具有动态名称idk。
考虑更深入地迭代<attribute>
标签:
foreach($product->attributes->attribute as $child) { //attributes
if ($child['name']=="Sample attribute") { $atr=$atr.$child['name'].': '.$child['value'].','; }
if ($child['name']=="Sample attribute 2") { $atr=$atr.$child['name'].': '.$child['value'].','; }
}
事实上,你甚至不需要if
语句:
foreach($product->attributes->attribute as $child) { //attributes
$atr=$atr.$child['name'].': '.$child['value'].',';
}
或者,由于需要将源 XML 转换为最终 XML,请考虑 XSLT,这是一种专门用于转换 XML 文档的特殊用途语言。在.ini文件中启用了扩展名的 PHP(扩展名=php_xsl.so 或扩展名 =php_xsl.dll)维护 XSLT 1.0 处理器。下面的 XSLT 连接图片和属性的子值(可扩展到 10+ 个节点):
XSLT(另存为 .xsl 文件,在下面的 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:template match="products">
<xsl:copy>
<xsl:apply-templates select="product"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product">
<xsl:copy>
<xsl:copy-of select="id|stock"/>
<pictures>
<xsl:for-each select="pictures">
<xsl:value-of select="concat(pic1, ', ', pic2)" />
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</pictures>
<attributes>
<xsl:for-each select="attributes/attribute">
<xsl:value-of select="concat(@name, ': ', @value)" />
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</attributes>
</xsl:copy>
</xsl:template>
</xsl:transform>
PHP(没有for循环,没有if语句,没有局部变量,没有意大利面条代码)
// LOAD XML AND XSL SOURCES
$xml = simplexml_load_file('Input.xml');
$xsl = simplexml_load_file('XSLTScript.xsl');
// CONFIGURE TRANSFORMER
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXML = $proc->transformToXML($xml);
echo $newXML;
// <?xml version="1.0" encoding="UTF-8"?>
// <products>
// <product>
// <id>1</id>
// <stock>23</stock>
// <pictures>linktopic1.jpg, linktopic2.jpg</pictures>
// <attributes>Sample attribute: Sample value, Sample attribute 2: Sample value 2</attributes>
// </product>
// </products>