2012-01-23 8 views
6

RSSフィードをレンダリングするための小さなカスタムXSLファイルを作成しています。内容は次のように基本的なものです。ソースXMLにフィード定義の 'xmlns = "http://www.w3.org/2005/Atom"という行が含まれている場合を除いて、これは完璧に機能します。これにどのように対処しますか?私は、この事件をどのように説明するのかを知るために、名前空間に慣れていません。AtomフィードのXSLの作成

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

答えて

8

あなたはこのように、XSLTに名前空間宣言を置く:ATOM名前空間接頭辞がatom:に登録され、スタイルシート全体のすべてのXPathで使用されていることを

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

注意。私はexclude-result-prefixesを使用して、atom:が結果のドキュメントに表示されないようにしました。

また、<xsl:for-each>をテンプレートに置き換えました。あなたはfor-eachを避けるようにして、テンプレートを優先してください。

disable-output-escaping="yes"の使用は、summaryの内容が正しい形式のXHTMLであることを絶対に肯定しない限り、XHTMLとやや危険です。

+0

XHTMLは安全であると確信しています。これは内部ソースからのものです。助けてくれてありがとう。 –

関連する問題