3

I want to convert xml data into html. Below is sample xml data and I want to get/convert it in html format.

<content type="html">
  <paragraph id="1291266887">
      <div class="red">
        <span id="main_post_id">
          <p>ten post przedstawia jak wys&#x142;a&#x107;  znacznik&#xF3;w w ust <strong>Ling</strong> -  xyz</p>
          <p>tags znane jako <span class="translation_section  section_2">bezpieczne</span>, b&#x119;d&#x105;  traktowane jako sekcje pkt</p>
          <p>innych materia&#x142;&#xF3;w dziel&#x105;  si&#x119; na <em>literach</em></p>
        </span>
      </div>
  </paragraph>
</content>

i.e. I want to get all the html code contained between <paragraph id="..."></paragraph>. I want to do it using php. When I convert it in array using php, it gives all data in array of div, p, span. But I want whole html contained in <paragraph>..</paragraph> tag in a single array.

Please help needed here. Let me know best ways for this.

Thanks.

Gordon
  • 312,688
  • 75
  • 539
  • 559
gautamlakum
  • 11,815
  • 23
  • 67
  • 90

3 Answers3

4

It's easy to use xslt in PHP. If you got the xml in file "data.xml" and the xslt-script in "script.xsl" this is all to do:

<?php  
$proc=new XsltProcessor;  
$proc->importStylesheet(DOMDocument::load("data.xsl"));  
echo $proc->transformToXML(DOMDocument::load("script.xml"));  
?>  

A simple xslt-script could look like this:

<?xml version="1.0" encoding="utf-8"?>  
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
 <xsl:output method="html" encoding="UTF-8" indent="yes"/>  
  <xsl:template match="/">  
   <html>  
    <head>  
    </head>  
    <body>  
     <xsl:copy-of select="//paragraph"/>  
    </body>  
   </html>  
  </xsl:template>  
</xsl:stylesheet>  
Andreas
  • 1,220
  • 8
  • 21
3

Write a XSLT stylesheet to convert it, and the xslt extension to apply it to the XML.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    I want to do it using php. When I convert it in array using php, it gives all data in array of div, p, span. But I want whole html contained in .. tag in a single array. – gautamlakum Dec 02 '10 at 06:52
  • 1
    Then you can't process it as XML, since processing it as XML will cause that. – Ignacio Vazquez-Abrams Dec 02 '10 at 06:54
  • 1
    I have just posted some part of xml data, not whole xml here. I am have converted it to array but not getting data contained in .. in a single array or array key. – gautamlakum Dec 02 '10 at 07:08
1

After running the following code in the browser save page , so the file is created in two stages

<?php
$xml=simplexml_load_file("My_Xml.xml");
print_r($xml);
?>
M.Ganji
  • 818
  • 8
  • 13