使用XSLT转换外部RSS提要

时间:2012-11-01 22:13:26

标签: xslt rss xml-parsing

我在wordpress上有一个博客,我希望能够抓取RSS源并使用XSLT将其解析到另一个网站。我正在尝试在客户端解析它,所以我可以插入到源的链接并利用它进行实时更新。

这是我的html:

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(dname){
if (window.XMLHttpRequest){
    xhttp=new XMLHttpRequest();
}else{
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

function displayResult(){
xml=loadXMLDoc("http://myBlog.wordpress.com/feed");
xsl=loadXMLDoc("styles.xsl");
// code for IE
if (window.ActiveXObject){
  ex=xml.transformNode(xsl);
  document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument){
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    document.getElementById("example").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="displayResult()">
<div id="example"></div>
</body>
</html>

这是我的xsl doc,我用来格式化我从博客中获取的Feed。当我查看HTML文件时,我没有收到任何错误,但页面只是空白,没有任何内容。

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:georss="http://www.georss.org/georss"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
xmlns:media="http://search.yahoo.com/mrss/">

<xsl:template match="/">
 <html>
 <body>
<xsl:for-each select="rss/channel/item">
    <p>
        <xsl:value-of select="title"/>
    </p>
</xsl:for-each>
 </body>
 </html>
</xsl:template>

0 个答案:

没有答案