如何在浏览器中查看 xsl 输出? [英] How to view xsl output in browsers?

查看:30
本文介绍了如何在浏览器中查看 xsl 输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!

我正在基于一些 xml 数据文件构建一个网站,所以我选择使用 XSLT 来绑定浏览器中的样式表.

一开始效果很好,但是最近,随着模板变得越来越复杂,发生了一些奇怪的事情.

我使用copy-of"元素将数据复制到样式表中.代码如下:

<xsl:copy-of select="article/bodydata/*"/>

所以,基本上,我将bodydata"节点的所有子元素复制到 <div/> 中.

但它不起作用.例如,我有一个 <img/>bodydata 中的元素,如果我在浏览器中访问 xml 文件,该图像不会出现.另一方面,如果我手动将bodydata"元素复制到该 div 中,并将 .xsl 文件转换为 .html 文件,则该图像确实会显示.

那么,这是我的问题,我可以在浏览器中查看 xml 数据和 xsl 数据的组合输出吗?我需要任何扩展或什么?

关于可能出什么问题的任何建议?我对 xslt 很陌生,所以似乎我误解了 XSLT 的真正作用.谢谢!

更新

为了说明问题,我写了一个小例子.

首先,我创建了一个示例 xml 数据文件:

所以,你可以看到,bodydata"元素中的所有节点都是需要在网页上显示的html元素.为了显示它,我创建了一个示例 xsl 文件.

<身体><文章><标题>文章标题</标题><部分><xsl:copy-of select="article/bodydata/*"/></节></文章><页脚>页面的页脚部分</页脚></html></xsl:模板></xsl:stylesheet>

结果是这样的:img 元素就消失了.

接下来,我将 bodydata 元素复制到 section 部分,并形成了一个新的 html 文件.

<头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="robots" content="noindex"/><身体><文章><标题>文章标题</标题><部分><中心><img alt="sample" src="http://www.google.com/logos/classicplus.png"/></中心><p><data class="tts_data">这是段落.</数据></p><p><data class="tts_data">这是第二段,</数据><data class="tts_data">更多数据</数据><data class="tts_data">...</data></p></节></文章><页脚>页面的页脚部分</页脚></html>

这里的结果是:图像出现.

所以我想知道这里出了什么问题.

解决方案

考虑向我们展示最少但完整的示例来演示问题,否则很难说.xsl:copy-of 对我来说听起来是正确的方法,假设 bodydata 元素将 HTML 元素作为子元素.至于查看转换结果,现在大多数浏览器都自带开发工具(或者你可以安装Firebug之类的),然后你可以按F12在浏览器中查看文档的DOM树来查看结果.至于复制到结果树的元素没有显示出来,一般来说可能是命名空间问题(即将 HTML 无命名空间的 img 复制到 XHTML 结果文档).因此,请向我们展示更多有关您尝试过的浏览器、您的 XSLT 具有哪种 output 方法、您尝试创建哪个版本的 HTML 作为转换结果(即 HTML 4.01、XHTML 1.0)的详细信息, HTML5).

在您当前的示例中,您在 XHTML 1.0 名称空间中使用名为 html 的根元素作为结果树,Firefox/Mozilla 将您的结果呈现为 XML,其中名称空间很重要.当您从输入的 XML 中复制 img 元素时,您将没有命名空间的 img 元素复制到 XHTML 文档中,因此由于 上缺少正确的命名空间>img 元素 浏览器无法将 img 元素识别为 XHTML img 元素.因此,您要么需要更改输入 XML 以将 XHTML 命名空间用于要复制的 (X)HTML 片段,或者,在我看来,这些天的重点是没有命名空间的 HTML 4 或 HTML5,您只需使用样式表结果树中的那个 HTML 版本,即您不为文档的根元素使用命名空间,您设置 <xsl:output method="html" version="4.01"/>.这样问题就会消失.

作为替代方案,如果您确实希望结果是 XHTML,但您想将没有命名空间的 XML img 元素复制到 XHTML 结果树中,那么正确的方法是不使用 copy-of,而您将使用 <xsl:apply-templates/> 然后您需要编写模板将您需要的元素转换为 XHTML 命名空间,例如

<xsl:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml"><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:element></xsl:模板>

在我看来,所有浏览器供应商都专注于没有命名空间的 HTML5,但我建议不要转换为 XHTML,而是针对没有命名空间的 HTML 4.01 或 HTML5,您可以在浏览器中使用 XSLT 1.0 获得更好的跨浏览器互操作性.

everyone!

I'm building a website based on some xml data files, so I chose to work with XSLT to bind the stylesheets in browsers.

It works quite good at first, but lately, as the templates grow more complicated, something strange happened.

I use the "copy-of" element to copy the data into the stylesheets. Here's the code:

<div class="section1">
    <xsl:copy-of select="article/bodydata/*" />
</div>

So, basically, I'm copying all child elements of the "bodydata" node into <div />.

But it doesn't work. For example, I've got an <img /> element in the bodydata, and that image does not appear if I visit the xml file in the browser. On the other hand, if I copy the "bodydata" elements by hand into that div, and make the .xsl file into a .html file, that image does show up.

So, here's my question, can I view the combined output of the xml data and the xsl data in browsers? Do I need any extension or what?

And any suggestions on what might be wrong? I'm quite new to xslt, so it seems that I misunderstood what XSLT really does. Thanks!

UPDATE

To illustrate the problem, I wrote a little sample.

First, I created a sample xml data file:

<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="article.xsl"?>
<article>
    <bodydata>
        <center>
            <img alt="sample" src="http://www.google.com/logos/classicplus.png" />
        </center>
        <p>
          <data class="tts_data">
          this is the paragraph.
          </data>
        </p>
        <p>
          <data class="tts_data">this is the second paragraph</data>
          <data class="tts_data">more data</data>
          <data class="tts_data">...</data>
        </p>
    </bodydata>
</article>

So, you can see, all nodes in "bodydata" element are html elements that needs to be show on webpage. In order to display that, I created a sample xsl file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <meta name="robots" content="noindex" />
        </head>
        <body>
          <article>
           <header>
                  header of the article
               </header>
               <section>
                  <xsl:copy-of select="article/bodydata/*" />
           </section>
      </article>
          <footer>
          footer part of the page
          </footer>
        </body>
     </html>
    </xsl:template>
</xsl:stylesheet>

And the result is like this: The img element just disappears.

And next, I copied the bodydata elements into the section part, and formed a new html file.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="robots" content="noindex" />
</head>
<body>
  <article>
    <header>
     header of the article
    </header>
    <section>
      <center>
        <img alt="sample" src="http://www.google.com/logos/classicplus.png" />
      </center>
      <p>
        <data class="tts_data">
        this is the paragraph.
        </data>
      </p>
      <p>
        <data class="tts_data">
        this is the second paragraph, 
        </data>
        <data class="tts_data">
        more data
        </data>
        <data class="tts_data">...</data>
      </p>
  </section>
    </article>
    <footer>
    footer part of the page
    </footer>
  </body>
</html>

And the result here is: And the image appears.

So I'm wondering what's wrong here.

解决方案

Consider to show us minimal but complete samples to demonstrate the problem, otherwise it is hard to tell. xsl:copy-of sounds like the right approach to me, assuming the bodydata element has HTML elements as the child elements. As for looking at the transformation result, these days most browsers come with a developer tool (or you can install one like Firebug), then you can hit F12 to see the DOM tree of the document in the browser to inspect the result. As for an element copied to the result tree not showing up, in general it could be a namespace problem (i.e. copying HTML namespace-less img to XHTML result document). So show us more details as to which browser(s) you have tried, which output method your XSLT has, which version of HTML you are trying to create as the transformation result (i.e. HTML 4.01, XHTML 1.0, HTML5).

[edit] With your current samples where you use a root element named html in the XHTML 1.0 namespace for the result tree, Firefox/Mozilla render your result as XML where namespaces matter. When you copy the img element from the input XML you copy an img element in no namespace into an XHTML document, so due to the lack of the right namespace on the img element the browser does not recognize the img element as an XHTML img element. So you either need to change your input XML to use the XHTML namespace for the fragments of (X)HTML you want to copy, or, in my view these days with the focus being on HTML 4 or HTML5 with no namespaces, you simply use that version of HTML in the result tree of the stylesheet i.e. you don't use a namespace for the document's root element, you set <xsl:output method="html" version="4.01"/> or <xsl:output method="html" version="5.0"/>. That way the problem should go away.

As an alternative, if you really want the result to be XHTML but you want to copy an XML img element in no namespace into the XHTML result tree, the proper way would be not to use copy-of, instead you would use <xsl:apply-templates/> and then you would need to write templates that transform the elements you need into the XHTML namespace e.g.

<xsl:template match="img | p | center">
  <xsl:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

In my view with all browser vendors focusing on HTML5 without namespaces I would however suggest not to transform to XHTML and instead target HTML 4.01 or HTML5 without namespaces, you get better cross-browser interoperability with XSLT 1.0 in the browser.

这篇关于如何在浏览器中查看 xsl 输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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