Save and Display a XML file created by transformin

2019-09-01 10:41发布

Hi I have a xml file and i'm transforming it's values from xsl file in php. I want to display the new values as an xml. I can get values echoed in the php but when I put the header it gives an error saying junk after element. and I cannot use saveXML() to the returned string. I need to save these returned information in a new xml file which I have no idea how to do. Please help me in this> thank you in advance.

php file

<?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 file

<?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>

标签: php xml xslt
1条回答
做个烂人
2楼-- · 2019-09-01 11:31

This code will help u to save xml string to a file.

<?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");
?>

Cheers.

查看更多
登录 后发表回答