为 Atom 提要创建 XSL [英] Creating XSL for Atom feed

查看:27
本文介绍了为 Atom 提要创建 XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小的自定义 XSL 文件来呈现 RSS 提要.内容是基本的,如下.除非源 XML 在提要定义中包含xmlns="http://www.w3.org/2005/Atom""行,否则这可以完美运行.我该如何解决这个问题?我对命名空间不够熟悉,不知道如何解释这种情况.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:template match="/" ><html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"><body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"><xsl:for-each select="feed/entry"><div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold"><xsl:value-of select="title"/></span>- <xsl:value-of select="author"/>

<div style="margin-left:20px;margin-bottom:1em;font-size:10pt"><b><xsl:value-of select="published"/></b><xsl:value-of select="summary" disable-output-escaping="yes"/>

</xsl:for-each></html></xsl:模板></xsl:stylesheet>

解决方案

您将命名空间声明放入 XSLT,如下所示:

</html></xsl:模板><xsl:template match="atom:entry"><div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold"><xsl:value-of select="atom:title"/></span><xsl:text>- </xsl:text><xsl:value-of select="atom:author"/>

<div style="margin-left:20px;margin-bottom:1em;font-size:10pt"><b><xsl:value-of select="atom:published"/></b><xsl:value-of select="atom:summary" disable-output-escaping="yes"/>

</xsl:模板></xsl:stylesheet>

请注意,ATOM 命名空间使用前缀 atom: 注册,并在整个样式表的所有 XPath 中使用.我使用了 exclude-result-prefixes 来确保 atom: 不会出现在结果文档中.

另请注意,我用模板替换了您的 .您也应该尽量避免使用 for-each 以支持模板.

disable-output-escaping="yes" 与 XHTML 结合使用有点危险 - 除非您绝对肯定 summary 的内容是好的-也形成了 XHTML.

I'm creating a small custom XSL file to render an RSS feed. The contents are basic, as follows. This works flawlessly except when the source XML contains the line 'xmlns="http://www.w3.org/2005/Atom"' in the feed definition. How do I address this? I'm not familiar enough with namespaces to know how to account this case.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/" >
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
  <xsl:for-each select="feed/entry">
      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of select="title"/></span> - <xsl:value-of select="author"/>
      </div>
      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <b><xsl:value-of select="published" /> </b>
        <xsl:value-of select="summary"  disable-output-escaping="yes" />
      </div>
    </xsl:for-each>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

解决方案

You put the namespace declaration into the XSLT, like this:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atom="http://www.w3.org/2005/Atom"
  exclude-result-prefixes="atom"
>
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
        <xsl:apply-tepmplates select="atom:feed/atom:entry" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="atom:entry">
    <div style="background-color:teal;color:white;padding:4px">
      <span style="font-weight:bold">
        <xsl:value-of select="atom:title"/>
      </span>
      <xsl:text> - </xsl:text>
      <xsl:value-of select="atom:author"/>
    </div>
    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
      <b><xsl:value-of select="atom:published" /> </b>
      <xsl:value-of select="atom:summary"  disable-output-escaping="yes" />
    </div>
  </xsl:template>
</xsl:stylesheet>

Note that the ATOM namespace is registered with the prefix atom: and used in all XPath throughout the stylesheet. I've used exclude-result-prefixes to make sure atom: will not show up in the resulting document.

Also note that I replaced your <xsl:for-each> with a template. You should try to avoid for-each in favor of templates, as well.

The use of disable-output-escaping="yes" is somewhat dangerous in conjunction with XHTML - unless you are absolutely positive that the contents of summary is well-formed XHTML, too.

这篇关于为 Atom 提要创建 XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆