为Atom提要创建XSL

时间:2012-01-23 15:48:49

标签: xml xslt

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

<?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>

1 个答案:

答案 0 :(得分:8)

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

<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>

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

另请注意,我已使用模板替换了您的<xsl:for-each>。你应该尽量避免为每个人提供模板。

使用disable-output-escaping="yes"与XHTML一起使用有点危险 - 除非你绝对肯定summary的内容也是结构良好的XHTML。

相关问题