使用PHP将xml转换为html [英] convert xml to html using php

查看:710
本文介绍了使用PHP将xml转换为html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xml数据转换为html。

 < content type =html>下面是示例xml数据,我想以html格式获取/转换它。 ; 
< paragraph id =1291266887>
< div class =red>
< span id =main_post_id>
< p>十篇文章, znacznik&#xF3; w w ust< strong> Ling< / strong> - xyz< / p>
< p>标签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>

即我希望获取包含在< paragraph id =...>< / paragraph> 之间的所有html代码。我想用php做。当我使用php将其转换为数组时,它将以div,p,span的数组形式提供所有数据。但我希望在单个数组中包含< paragraph> ..< / paragraph> 标记中的整个html。



<请在这里帮助。让我知道最好的方法。



谢谢。

解决方案

在PHP中使用xslt很容易。如果你在文件data.xml中获得了xml并且在script.xsl中获得了xslt-script,那么这就是所有要做的事情:

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

一个简单的xslt脚本可能如下所示:

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


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.

解决方案

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>  

这篇关于使用PHP将xml转换为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆