我必须将 xml 输出转换为 html。我正在关注以下教程链接文本我的代码在一行中输出 xml 标签,没有任何文本值格式。我希望输出为 HTML 无序列表,遵循 xml 文件输出中父子级的层次结构。
这是页面输出:
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
& 这是页面源:
<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.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>
消息.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>
您的样式表不输出 HTML,而是输出 XHTML 片段,并且您需要以某种方式(使用限定名称)将其作为 application/xml 提供给浏览器(例如 Mozilla、Opera、Safari、IE 9,但不是 IE 6) -8) 理解该内容类型。
所以请确保你做了类似的事情
header('Content-Type: application/xml');
在将内容发送到浏览器之前。或者从结果元素中删除任何 XHTML 命名空间和任何前缀,然后 XSLT 样式表输出一个 HTML 片段,更多浏览器可以解析和理解为 text/html 并根据需要呈现它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)