我正在创建一个小的自定义 XSL 文件来呈现 RSS 提要。内容是基本的,如下。除非源 XML 在提要定义中包含行 '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>
您将命名空间声明放入 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-templates 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>
与模板。您也应该尽量避免使用 for-each 而使用模板。
指某东西的用途disable-output-escaping="yes"
与 XHTML 结合起来有些危险 - 除非您绝对肯定 XHTML 的内容summary
也是格式良好的 XHTML。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)