保存并显示通过使用Xsl文件转换另一个XML的值而创建的XML文件


Save and Display a XML file created by transforming values of another XML using Xsl file.

嗨,我有一个xml文件,我正在转换它的值从xsl文件在php。我希望将新值显示为xml格式。我可以得到值回显在php中,但当我把标题,它给出了一个错误,说垃圾元素后。我不能对返回的字符串使用saveXML()。我需要将这些返回的信息保存在一个新的xml文件中,我不知道如何做。请在这方面帮助我,提前谢谢你。

php文件

<?php
//header('Content-Type: text/xml');
$xmlDoc = new DOMDocument('1.0');
$xmlDoc->formatOutput = true;
$xmlDoc->load("rental.xml");
$xslDoc = new DomDocument('1.0');
$xslDoc->load("apartment.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
$strXml= $proc->transformToXML($xmlDoc);
//echo (toXml($strXml));
echo ($strXml);
//echo $strXml->saveXML();
?>
xsl文件

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output  method="xml" indent="yes" />   
  <xsl:template match="/">
  <xsl:for-each select="//property">
      <xsl:element name="rentalProperties">
        <xsl:element name="description">
        <xsl:value-of select="description"/>
        </xsl:element>
      </xsl:element>
    </xsl:for-each>  
  </xsl:template>
</xsl:stylesheet>

这段代码将帮助您将xml字符串保存到文件中。

<?php
//header('Content-Type: text/xml');
$xmlDoc = new DOMDocument('1.0');
$xmlDoc->formatOutput = true;
$xmlDoc->load("rental.xml");
$xslDoc = new DomDocument('1.0');
$xslDoc->load("apartment.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
$strXml= $proc->transformToXML($xmlDoc);
//echo (toXml($strXml));
echo ($proc->transformToXML($xmlDoc));
//$strXml->saveXML();
// Way to parse XML string and save to a file
$convertedXML = simplexml_load_string($strXml);
$convertedXML->saveXML("member.xml");
?>

欢呼。