使用XSL和Php将XML数据转换为HTML:仅打印没有任何格式的xml标记 [英] Transforming XML data to HTML using XSL and Php: only printing xml tags without any formatting

查看:72
本文介绍了使用XSL和Php将XML数据转换为HTML:仅打印没有任何格式的xml标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将我的xml输出转换为html。我正在链接文本中阅读教程我的代码正在输出xml标签单行没有任何格式与文本value.I希望输出作为一个HTML无序列表跟随在父文件的层次结构在xml文件输出。



这是页面输出:

 消息消息1消息消息1-1消息消息1-2消息消息1-2-1消息消息1-2-2消息
msg 1-2-2-1message msg 1-2-2-1-1message msg 1-2-2-1-2

&这里是页面源代码:

 < html:ul xmlns:html =http://www.w3.org/1999 / xhtml>< html:li> message msg 1< / html:li> 
< html:ul>< html:li> message msg 1-1< / html:li>< html:li> message msg 1-2< / html:li>< html:ul>
< html:li> message msg 1-2-1< / html:li>< html:li> message msg 1-2-2< / html:li>< html:ul>
< html:li> message msg 1-2-2-1< / html:li>< html:ul>< html:li> message msg 1-2-2-1-1< HTML:LI>
< html:li> message msg 1-2-2-1-2< / html:li>< / html:ul>< / html:ul>< / html:ul>< / HTML:UL>< / HTML:UL>

这是我的代码

  php file:

<?php
#LOAD XML FILE
$ XML = new DOMDocument();
$ XML-> load('messages.xml');

#START XSLT
$ xslt = new XSLTProcessor();
$ XSL = new DOMDocument();
$ XSL-> load('msg.xsl');
$ xslt-> importStylesheet($ XSL);
print $ xslt-> transformToXML($ XML);
?>

msg.xsl:

 <?xml version =1.0encoding =utf-8?> 
xmlns:html =http:// www。 w3.org/1999/xhtml\">
< xsl:output omit-xml-declaration =yes/>

< xsl:template match =messages>
< html:ul>
< xsl:apply-templates select =message/>
< / html:ul>
< / xsl:template>

< xsl:template match =message [message]>
< html:li>讯息< xsl:value-of select =@ msg_id/>< / html:li>
< html:ul>
< xsl:apply-templates select =message/>
< / html:ul>
< / xsl:template>

< xsl:template match =message>
< html:li>讯息< xsl:value-of select =@ msg_id/>< / html:li>
< xsl:apply-templates select =message/>
< / xsl:template>
< / xsl:stylesheet>

messages.xml

 <?xml version =1.0?> 
<?xml-stylesheet type =text / xslhref =msg.xsl?>< messages>
< message msg_id =1emp_msg =msg 1parent_msg_id =parent_msg =depth =0>
< message msg_id =3emp_msg =msg 1-2parent_msg_id =1parent_msg =msg 1depth =1>
< message msg_id =5emp_msg =msg 1-2-2parent_msg_id =3parent_msg =msg 1-2depth =2>
gt ;
gt ;
< / message>
< / message>
< / message>
< / message>
< /消息>


解决方案

您的样式表不会输出HTML,而是一个XHTML片段,并以某种方式(使用限定名称)将其作为application / xml提供给了解该内容类型的浏览器(如Mozilla,Opera,Safari,IE 9,但不包括IE 6-8)。

所以请确保你做了类似于

  header('Content -Type:application / xml'); 

,然后将内容发送到浏览器。或者从结果元素中删除任何XHTML名称空间和任何前缀,然后XSLT样式表输出一个HTML片段,许多浏览器可以解析和理解为text / html并根据需要进行渲染。


I have to transform my xml output to html. I am following a tutorial at link text My code is outputting xml tags in a single line without any formatting with text value.I want the output as an HTML unordered list following hierarchical structure of parent child in xml file output.

Here is page output:

message msg 1message msg 1-1message msg 1-2message msg 1-2-1message msg 1-2-2message 
msg 1-2-2-1message msg 1-2-2-1-1message msg 1-2-2-1-2 

& here is page source:

<html:ul xmlns:html="http://www.w3.org/1999/xhtml"><html:li>message msg 1</html:li>
<html:ul><html:li>message msg 1-1</html:li><html:li>message msg 1-2</html:li><html:ul>
<html:li>message msg 1-2-1</html:li><html:li>message msg 1-2-2</html:li><html:ul>
<html:li>message msg 1-2-2-1</html:li><html:ul><html:li>message msg 1-2-2-1-1</html:li>
<html:li>message msg 1-2-2-1-2</html:li></html:ul></html:ul></html:ul></html:ul></html:ul>

Here is my code

php file:

<?php
# LOAD XML FILE
$XML = new DOMDocument();
$XML->load('messages.xml');

# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load('msg.xsl');
$xslt->importStylesheet( $XSL );
print $xslt->transformToXML( $XML );
?>

msg.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:html="http://www.w3.org/1999/xhtml">
  <xsl:output omit-xml-declaration="yes" />

  <xsl:template match="messages">
    <html:ul>
      <xsl:apply-templates select="message" />
    </html:ul>
  </xsl:template>

  <xsl:template match="message[message]">
    <html:li>message <xsl:value-of select="@msg_id" /></html:li>
    <html:ul>
      <xsl:apply-templates select="message" />
    </html:ul>
  </xsl:template>

  <xsl:template match="message">
    <html:li>message <xsl:value-of select="@msg_id" /></html:li>
    <xsl:apply-templates select="message" />
  </xsl:template>
</xsl:stylesheet>

messages.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="msg.xsl"?><messages>
    <message msg_id="1" emp_msg="msg 1" parent_msg_id="" parent_msg="" depth="0">
        <message msg_id="2" emp_msg="msg 1-1" parent_msg_id="1" parent_msg="msg 1" depth="1"/>
        <message msg_id="3" emp_msg="msg 1-2" parent_msg_id="1" parent_msg="msg 1" depth="1">
            <message msg_id="4" emp_msg="msg 1-2-1" parent_msg_id="3" parent_msg="msg 1-2" depth="2"/>
            <message msg_id="5" emp_msg="msg 1-2-2" parent_msg_id="3" parent_msg="msg 1-2" depth="2">
                <message msg_id="6" emp_msg="msg 1-2-2-1" parent_msg_id="5" parent_msg="msg 1-2-2" depth="3">
                    <message msg_id="7" emp_msg="msg 1-2-2-1-1" parent_msg_id="6" parent_msg="msg 1-2-2-1" depth="4"/>
                    <message msg_id="8" emp_msg="msg 1-2-2-1-2" parent_msg_id="6" parent_msg="msg 1-2-2-1" depth="4"/>
                </message>
            </message>
        </message>
    </message>
</messages>

解决方案

Your stylesheet does not output HTML but rather an XHTML fragment, and that in a way (with qualified names) that you need to serve it as application/xml to a browser (like Mozilla, Opera, Safari, IE 9, but not IE 6-8) that understands that content type.

So make sure you do something like

  header('Content-Type: application/xml');

before sending the content to the browser. Or drop any XHTML namespace and any prefixes from the result elements, then the XSLT stylesheet outputs an HTML fragment many more browsers can parse and understand as text/html and render it as you want.

这篇关于使用XSL和Php将XML数据转换为HTML:仅打印没有任何格式的xml标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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