我正在处理一个项目,我必须显示一个带有样式表的XML文件。当我运行这个XML文件而不是显示MyPhpAdmin中的所有数据时,它只显示了其中一个数据。
下面是我有什么ProductController:
<?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>
我希望有人能帮我解决目前的状况
1条答案
按热度按时间n53p2ov01#
好吧,PHP创建的XML似乎是一种平面格式,每个“product”都没有 Package 器元素,因此您尝试使用
<xsl:for-each select="ProductDetails">
实际上只处理一个元素,并且任何xsl:value-of
都将输出一个值,例如第一个id。因此,我建议更改PHP,输出一些带有根元素(如ProductDetails
)的XML,然后让foreach ($productdatas as $productdata) {
编写一个Product
Package 器元素(例如$xml->startElement('Product');
),这样您就可以调整XSLT来处理<xsl:for-each select="ProductDetails/Product">
等数据。