Creating an XSL Channel for Atom

I am creating a small custom XSL file to render an RSS feed. The content is basic as follows. This only works flawlessly if the source XML contains the string "xmlns =" ​​http://www.w3.org/2005/Atom "in the feed definition. How can I answer this? I am not familiar 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> 
+6
source share
1 answer

You put a namespace declaration in XSLT, for example:

 <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 atom: prefix and is used in all XPath throughout the stylesheet. I used exclude-result-prefixes to make sure atom: will not appear in the resulting document.

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

Using disable-output-escaping="yes" somewhat dangerous when combined with XHTML - unless you are absolutely sure that the contents of summary also well-formed by XHTML.

+8
source

Source: https://habr.com/ru/post/906678/


All Articles