我正在做一个项目,我必须显示一个带有样式表的 XML 文件。当我运行 XML 文件而不是显示我在 MyPhpAdmin 中的所有数据时,它只显示其中一个数据。
这就是我所拥有的 产品负责人:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \XMLWriter as XMLW;
use App\Models\Product;
use Illuminate\Support\Facades\Storage;
class ProductController {
public function ProductXsl()
{
return response()->file(public_path('assets\images\ProductDetails.xsl'));
}
public function productXML() {
$productdatas = Product::all();
$xml = new XMLW();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->writePI('xml-stylesheet', 'type="text/xsl" href="ProductDetails.xsl"');
$xml->startElement('ProductDetails');
foreach ($productdatas as $productdata) {
$xml->startElement('ID');
$xml->text($productdata->id);
$xml->endElement();
$xml->startElement('Name');
$xml->text($productdata->name);
$xml->endElement();
$xml->startElement('Slug');
$xml->text($productdata->slug);
$xml->endElement();
$xml->startElement('Short_Description');
$xml->text($productdata->short_description);
$xml->endElement();
$xml->startElement('Description');
$xml->text($productdata->description);
$xml->endElement();
$xml->startElement('Regular_Price');
$xml->text($productdata->reqular_price);
$xml->endElement();
$xml->startElement('Sales_Price');
$xml->text($productdata->sale_price);
$xml->endElement();
$xml->startElement('SKU');
$xml->text($productdata->SKU);
$xml->endElement();
$xml->startElement('Stock_Status');
$xml->text($productdata->stock_status);
$xml->endElement();
$xml->startElement('Featured');
$xml->text($productdata->featured);
$xml->endElement();
$xml->startElement('Quantity');
$xml->text($productdata->quantity);
$xml->endElement();
// $xml->startElement('Image');
// $xml->text($productdata->image);
// $xml->endElement();
$xml->startElement('CategoryID');
$xml->text($productdata->category_id);
$xml->endElement();
$xml->startElement('CreatedAt');
$xml->text($productdata->created_at);
$xml->endElement();
$xml->startElement('UpdatedAt');
$xml->text($productdata->updated_at);
$xml->endElement();
}
$xml->endElement();
$xml->endDocument();
$content = $xml->outputMemory();
Storage::put('ProductDetails.xml', $content);
$xml = null;
return response()->file(public_path('assets\images\ProductDetails.xml'));
}
}
我的xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Product Details</title>
</head>
<body>
<h1>Product Information</h1>
<hr />
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Slug</th>
<th>Short Description</th>
<th>Description</th>
<th>Regular Price</th>
<th>Sales Price</th>
<th>SKU</th>
<th>Stock Status</th>
<th>Featured</th>
<th>Quantiy</th>
<th>Image</th>
<th>Category ID</th>
<!-- <th>Created At</th>
<th>Updated At</th> -->
</tr>
<xsl:for-each select="ProductDetails">
<xsl:sort select="ID" />
<tr>
<td><xsl:value-of select="ID" /></td>
<td><xsl:value-of select="Name" /></td>
<td><xsl:value-of select="Slug" /></td>
<td><xsl:value-of select="Short_Description" /></td>
<td><xsl:value-of select="Description" /></td>
<td><xsl:value-of select="Regular_Price" /></td>
<td><xsl:value-of select="Sales_Price" /></td>
<td><xsl:value-of select="SKU" /></td>
<td><xsl:value-of select="Stock_Status" /></td>
<td><xsl:value-of select="Featured" /></td>
<td><xsl:value-of select="Quantity" /></td>
<td><xsl:value-of select="Category_ID" /></td>
<!-- <td><xsl:value-of select="@Created_At" /></td>
<td><xsl:value-of select="@Updated_At" /></td> -->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我要运行的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ProductDetails.xsl"?>
<ProductDetails>
<ID>
46
</ID>
<Name>
Box 1
</Name>
<Slug>
box-1
</Slug>
<Short_Description>
<p>Box 1 (20 per Pack)</p>
</Short_Description>
<Description>
<p>Cardboard box - prefabricated for packaging</p>
<p>Size: 170mm(L) x 170mm(W) x 170mm(H)</p>
<p>Note: RM58/packCardboard box - prefabricated for packaging</p>
<p>Size: 170mm(L) x 170mm(W) x 170mm(H)</p>
<p>Note: RM58/pack</p>
</Description>
<Regular_Price>
58.00
</Regular_Price>
<Sales_Price>
58.00
</Sales_Price>
<SKU>
B1
</SKU>
<Stock_Status>
instock
</Stock_Status>
<Featured>
0
</Featured>
<Quantity>
100
</Quantity>
<CategoryID>
55
</CategoryID>
<CreatedAt>
2022-04-02 08:22:04
</CreatedAt>
<UpdatedAt>
2022-04-02 08:22:04
</UpdatedAt>
<ID>
47
</ID>
<Name>
Box2
</Name>
<Slug>
box2
</Slug>
<Short_Description>
<p>Box 2 (20 per Pack)</p>
</Short_Description>
<Description>
<p>Cardboard box - prefabricated for packaging</p>
<p>Size: 210mm(L) x 210mm(W) x 210mm(H)</p>
<p>Note: RM78/pack</p>
</Description>
<Regular_Price>
78.00
</Regular_Price>
<Sales_Price>
78.00
</Sales_Price>
<SKU>
B2
</SKU>
<Stock_Status>
instock
</Stock_Status>
<Featured>
0
</Featured>
<Quantity>
100
</Quantity>
<CategoryID>
55
</CategoryID>
<CreatedAt>
2022-04-04 16:36:52
</CreatedAt>
<UpdatedAt>
2022-04-05 08:34:01
</UpdatedAt>
希望有人能帮我解决目前的情况
好吧,您的 XML 创建的 PHP 似乎是一种平面格式,没有每个“产品”的包装元素,因此您尝试使用
<xsl:for-each select="ProductDetails">
确实处理单个元素,任何 xsl:value-of
将输出一个值,例如第一个ID。所以我建议更改 PHP 以输出一些带有根元素的 XML,例如 ProductDetails
,然后让 foreach ($productdatas as $productdata) {
为您在那里输出的所有数据编写一个 Product
包装元素(例如 $xml->startElement('Product');
),这样您可以调整您的 XSLT 以处理例如<xsl:for-each select="ProductDetails/Product">
.