2012-01-23 30 views
6

Tôi đang tạo tệp XSL tùy chỉnh nhỏ để hiển thị nguồn cấp dữ liệu RSS. Nội dung cơ bản, như sau. Điều này hoạt động hoàn hảo trừ khi XML nguồn chứa dòng 'xmlns = "http://www.w3.org/2005/Atom"' trong định nghĩa nguồn cấp dữ liệu. Làm cách nào để giải quyết vấn đề này? Tôi không đủ quen thuộc với các không gian tên để biết cách giải thích trường hợp này.Tạo XSL cho nguồn cấp dữ liệu 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> 

Trả lời

8

Bạn đặt tờ khai namespace vào XSLT, như thế này:

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

Lưu ý rằng không gian tên ATOM được đăng ký với tiền tố atom: và được sử dụng trong tất cả các XPath trong suốt stylesheet. Tôi đã sử dụng exclude-result-prefixes để đảm bảo atom: sẽ không hiển thị trong tài liệu kết quả.

Cũng lưu ý rằng tôi đã thay thế <xsl:for-each> bằng mẫu của bạn. Bạn nên cố gắng tránh cho mỗi người ủng hộ các mẫu, là tốt.

Việc sử dụng disable-output-escaping="yes" hơi nguy hiểm khi kết hợp với XHTML - trừ khi bạn hoàn toàn tích cực rằng nội dung của summary cũng là XHTML được định dạng tốt.

+0

Tôi chắc chắn rằng XHTML là an toàn - nó là từ một nguồn nội bộ. Cảm ơn sự hỗ trợ. –

Các vấn đề liên quan