Usted debe en este laboratorio programar un lector de RSS 1.0 en ASP.NET.
Para ello debe utilizar la función que se le da a continuación en una página:
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ import namespace="System.Net" %>
<script language="VB" runat="server">
Private Sub displayRSS(ByVal strSource As String, ByVal strXSL As String)
Dim wrRequest As WebRequest, wrResponse As WebResponse
Dim xmlReader As XmlTextReader, xmlWriter As XmlTextWriter
Dim xPathDoc As XPathDocument, xslt As New XslTransform()
Try
' Crea una conexion
wrRequest = WebRequest.Create(strSource)
' Use a proxy if required.
wrRequest.Proxy = New WebProxy("163.178.16.7:9003/")
wrResponse = wrRequest.GetResponse()
' Crea un escritor y un lector XML
xmlWriter = New XmlTextWriter(Response.Output)
xmlReader = New XmlTextReader(wrResponse.GetResponseStream())
' Abre como documento XPath
xPathDoc = New XPathDocument(xmlReader)
' Lee la hoja de estilo XSL
xslt.Load(Server.MapPath(strXSL))
' Despliega el contenido
xslt.Transform(xPathDoc, Nothing, xmlWriter)
Catch strErr As Exception
Response.Write("<p>" & strErr.ToString() & "</p>")
End Try
End Sub
</script>
O puede utlizar esta otra función (la que le sirva mejor):
Function displayRSS(ByVal strSource, ByVal strXSL)
Dim xmlHTTP, xmlRSSSource, xmlStyle
Dim strXSLFile
' Utiliza el objeto XMLHTTPConnection para tomar el XML
Set xmlHTTP = Server.CreateObject("Microsoft.XMLHTTP")
xmlHTTP.Open "GET", strSource, False
xmlHTTP.Send
' Carga el XML en un objeto DOM
Set xmlRSSSource = Server.CreateObject("Microsoft.XMLDOM")
xmlRSSSource.async = False
xmlRSSSource.loadXML(xmlHTTP.ResponseText)
' Lee la hoja de estilo XSL
strXSLFile = Server.MapPath(strXSL)
Set xmlStyle = Server.CreateObject("Microsoft.XMLDOM")
xmlStyle.async = False
xmlStyle.load(strXSLFile)
' Despliega utilizando la hoja de estilo
displayRSS = xmlRSSSource.transformNode(xmlStyle)
' Libera los recursos
Set xmlStyle = Nothing
Set xmlRSSSource = Nothing
Set xmlHTTP = Nothing
End Function
Como puede notar se necesita una hoja de estilo XSL, la cual se provee a continuación:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:rss="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/rdf:RDF">
<p>
<a><xsl:attribute name="href"><xsl:value-of select="rss:channel/rss:link"/></xsl:attribute>
<xsl:value-of select="rss:channel/rss:title"/></a>
</p>
<p>
<!-- Hace los datos presentables -->
<xsl:variable name="datetime" select="rss:channel/dc:date"/>
<xsl:variable name="day" select="substring($datetime, 9, 2)"/>
<xsl:variable name="month" select="substring($datetime, 6,
2)"/>
<xsl:variable name="year" select="substring($datetime, 0,
5)"/>
<xsl:value-of select="concat($day, '/', $month, '/', $year)"/>
-
<xsl:value-of select="substring($datetime, 12, 5)"/>
</p>
<dl>
<xsl:for-each select="rss:item">
<dt>
<a><xsl:attribute name="href"><xsl:value-of select="rss:link"/></xsl:attribute>
<xsl:value-of select="rss:title"/></a>
</dt>
<dd>
<xsl:value-of select="rss:description"/>
<!-- Formatea la fecha de publicación -->
(<xsl:variable name="pubdate" select="dc:date"/>
<xsl:variable name="pubday" select="substring($pubdate, 9,
2)"/>
<xsl:variable name="pubmonth" select="substring($pubdate,
6, 2)"/>
<xsl:variable name="pubyear" select="substring($pubdate, 0,
5)"/>
<xsl:value-of select="concat($pubday, '/', $pubmonth, '/', $pubyear)"/>
-
<xsl:value-of select="substring($pubdate, 12, 5)"/>)
</dd>
</xsl:for-each>
</dl>
<p>
<xsl:value-of select="rss:channel/dc:rights"/>
</p>
</xsl:template>
</xsl:stylesheet>
Mejore la hoja de estilo XSL para que tenga una mejor presentación.
Luego puede desplegar los datos con solamente:
<% displayRSS("http://direccion", "archivo.xsl") %>