【问题标题】:How to correctly use output as XML in XSLT?如何在 XSLT 中正确使用输出作为 XML?
【发布时间】:2013-02-28 22:55:31
【问题描述】:

我正在将我的 XSLT 输出为 HTML,但我读到出于验证原因,输出为 XML 更好。因此,当我将输出从 HTML 更改为 XML 时,它会破坏页面,这是否意味着我现在需要从 XSLT 文件中删除所有 HTML 元素?我还通过外部 CSS 文件进行样式设置,当输出为 XML 时,样式是否需要在 XSLT 文件中?

这是我的页面代码:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 
method="xml" 
doctype-system="http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"  
doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.2//EN" />

<xsl:template match="/">


        <xsl:element name="head">
            <xsl:element name="title">Selected Flight Route</xsl:element>
            <link rel="stylesheet" type="text/css" href="mystyles.css" title="Style"/>
        </xsl:element>

        <xsl:element name="body"> 

            <xsl:element name="div"><!-- This holds the navigation bar-->
                <xsl:attribute name="id">
                    <xsl:text>navdiv</xsl:text>
                </xsl:attribute>


                <xsl:element name="div"><!-- This holds the image links-->
                    <xsl:attribute name="id">
                        <xsl:text>navlinks</xsl:text>
                    </xsl:attribute>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksone</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">flights.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinkstwo</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">planes.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksthree</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">weatherfeed.php</xsl:attribute>
                    </xsl:element>

                </xsl:element>
            </xsl:element><!-- End the navigation bar-->






<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>maindiv</xsl:text>
                        </xsl:attribute>



<h1 id="heading">Flights and planes</h1>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divone</xsl:text>
                        </xsl:attribute><img width="30" height="30" src="globe.png"/><p class="centerp">Selecting the Globe will display all flight routes.</p></xsl:element>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divtwo</xsl:text>
                        </xsl:attribute><img width="75" height="30" src="planepic.png"/><p class="centerp">Selecting the Plane will display all Aircraft info.</p></xsl:element>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divthree</xsl:text>
                        </xsl:attribute><img width="32" height="30" src="weather.png"/><p class="centerp">The Weather icon displays the weather in all citys.</p></xsl:element>
</xsl:element>







        </xsl:element>
        <!-- End the Body element -->



</xsl:template>
<!-- End the Main Template element -->

</xsl:stylesheet>

Here 是指向输出为 XML 的页面的链接,here 是输出为 HTML 的同一页面,谁能告诉我为什么在输出为 XML 时这会弄乱我的页面?在研究问题是什么时,我注意到输出为 XML 的示例文件中似乎没有任何 HTML 元素。

这是我用来转换 XML 的原始文件:

<?php
$xml = new DOMDocument();   
$xml->load('flights.xml');

$xsl = new DOMDocument;
$xsl->load('index.xsl');

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
?>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    简答

    您不能将 XML 输出到浏览器并期望他呈现 HTML,它们是不同的语言。

    长答案

    如果您输出 XML 而不是 HTML,浏览器将不会按预期呈现页面,因为创建的是 XML 节点而不是 HTML 节点。这两者之间的区别在于 HTML 节点比 XML 节点具有更多的属性。例如。其中一些属性允许我们设置 HTML 元素的样式。

    XML 节点的属性允许我们操作和导航 DOM,而 HTML 属性是所有 XML 属性的超集(我认为)。

    对于每种节点可用的属性列表(从 Firefox 的角度来看,它非常符合标准):https://developer.mozilla.org/en-US/docs/DOM/element

    XHTML

    因为您指的是验证,所以 HTML 4.01 的替代方案是 XHTML 1.0,它是与 HTML 或 XML 不同的语言,并且在验证方面更严格(例如,它不允许不匹配的标签)。

    要指示浏览器您使用的是 XHTML 1.0 而不是 HTML 4.01,您必须在文档顶部包含这些 DOCTYPE 元素之一。

     <!DOCTYPE html 
         PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <!DOCTYPE html 
         PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <!DOCTYPE html 
         PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
    

    CSS 差异

    在如何处理 XHTML 文档的 CSS 方面也存在一些(细微的)差异。我给你留下一篇文章,列出这些差异:

    http://reference.sitepoint.com/css/htmlxhtml

    【讨论】:

    • 谢谢,我一直在努力解决这个问题,我只是发现是 Doctype 在我开发移动网站时导致了这个问题,当我使用移动 Doctype 时,它​​会把页面搞砸。您知道如何在我的 XSLT 中使用移动 XHTML Doctype 吗?谢谢!我正在尝试在此页面上使用其中之一mobiforge.com/testing/story/fixit-xhtml-mobile-profile
    • 好吧,我从未使用过 XHTML 移动设备,对此我了解不多,但您可以阅读这些文章/书的片段的第二部分:xml.com/pub/a/2004/04/14/mobile.html 在那里他们只使用外部 DTD。希望对你有帮助
    • 感谢您的帮助,我尝试了所有类型的移动 DTD,但没有任何效果。当我使用 XHTML 严格的 DTD 时,我的页面加载正常并且一切都有效,但是当我使用移动 DTD 时,它把一切都搞砸了,我不知道为什么。似乎没有太多关于移动 DTD 和 XSLT 的信息!我会继续阅读!
    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多