[英]Save XML after XSL transformation using PHP
I transform an XML file into another XML file using an XSL file in a PHP page.我在 PHP 页面中使用 XSL 文件将一个 XML 文件转换为另一个 XML 文件。 For this, I use DOMDocument by passing the XML file and the XSL file as parameters.为此,我通过将 XML 文件和 XSL 文件作为参数传递来使用 DOMDocument。
The transformation works but the UTF-8 characters are replaced in the output XML file.转换有效,但 UTF-8 字符在 output XML 文件中被替换。 However, my original XML file is in UTF-8 and so is my XSL sheet.但是,我原来的 XML 文件在 UTF-8 中,我的 XSL 工作表也是如此。
The simplexml_load_string function encodes the accents in UTF-8. But when saving the file with the saveXML function, the created file does not contain the accents. simplexml_load_string function 将重音编码为 UTF-8。但是当使用 saveXML function 保存文件时,创建的文件不包含重音。 I don't understand why UTF-8 doesn't work.我不明白为什么 UTF-8 不起作用。 Do you have an idea?你有好主意吗?
Here is an example of an input XML file:以下是输入 XML 文件的示例:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Transfer xmlns="dase:v2.1" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:id="_20220325095723763" xsi:schemaLocation="dase:v2.1 main.xsd">
<Message>test</Message>
<CodeList>
<Element>villé</Element>
</CodeList>
</Transfer>
Here is my XSL file:这是我的 XSL 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.1" xmlns:dase="dase:v2.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns2="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="dase:v2.1" exclude-result-prefixes="dase">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dase:Message">
<xsl:comment>
<xsl:text>New value</xsl:text>
</xsl:comment>
</xsl:template>
</xsl:stylesheet>
Here is my PHP code:这是我的 PHP 代码:
$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$xmlDoc->formatOutput = true;
$xmlDoc->encoding = 'UTF-8';
$xmlDoc->load("./uploads/" . $fileName);
$xmlDoc->encoding = 'UTF-8';
$xslDoc = new DomDocument('1.0');
$xslDoc->load("./xslt/file.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
$strXml = $proc->transformToXML($xmlDoc);
//echo ($proc->transformToXML($xmlDoc)); //here, the accent is fine
$convertedXML = simplexml_load_string($strXml);
$convertedXML->encoding = 'UTF-8';
//print_r($convertedXML); //here, the accent is fine
$convertedXML->encoding = 'UTF-8';
$convertedXML->saveXML("./uploads/Cleaned_" . $fileName); //the save file have accent problem
Thanks in advance提前致谢
You can use html_entity_decode()
to decode HTML entities and get back the accentued characters:您可以使用 html_entity_decode html_entity_decode()
解码 HTML 个实体并取回重音字符:
$outputFilename = "./Cleaned_" . $fileName ;
$output = $convertedXML->saveXML(); // get the XML file content
$output = html_entity_decode($output, ENT_NOQUOTES, 'UTF-8'); // decode HTML characters
file_put_contents($outputFilename, $output); // write decoded content to disk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.