使用XSL在HTML页面上显示XML的超链接

时间:2012-01-20 15:03:25

标签: javascript html xml

我正在尝试将XML文件中的网址显示在HTML网页上作为可点击的超链接,而不仅仅是将网址显示为文字。

如何做到这一点?我很确定我所做的并不遥远......

这是代码的样子:

XML

<linkedin>
<discussion>
    <topic>This is the discussion name</topic>
    <content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ligula mi, convallis eget iaculis id, euismod non arcu. Morbi porta.</content>
    <url>http://www.google.com</url>
</discussion>
</linkedin>

XSL

 <xsl:for-each select="linkedin/discussion">
    <h3><xsl:value-of select="topic"/></h3>
    <p><xsl:value-of select="content"/></p>
    <p><a><xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute><xsl:value-of select="url"/></a></p>
  </xsl:for-each>

我在 HTML 中使用JavaScript来提取XML文件中的内容

  <script type="text/javascript">
 if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","linkedin.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

var x=xmlDoc.getElementsByTagName("discussion");
for (i=0;i<x.length;i++)
{
document.write("<p><strong>");
document.write(x[i].getElementsByTagName("topic")[0].childNodes[0].nodeValue);
document.write("</strong></p><p>");
document.write(x[i].getElementsByTagName("content")[0].childNodes[0].nodeValue);
document.write("</p><p>");
document.write(x[i].getElementsByTagName("url")[0].childNodes[0].nodeValue);
}
document.write("</p>");
</script>

以下是它的样子...... URL只是文字,而不是超链接:

enter image description here

1 个答案:

答案 0 :(得分:0)

解决。在我的HTML中制作了JavaScript:

document.write("<p><strong>");
document.write(x[i].getElementsByTagName("topic")[0].childNodes[0].nodeValue);
document.write("</strong></p><p>");
document.write(x[i].getElementsByTagName("content")[0].childNodes[0].nodeValue);
document.write("</p><p>");
document.write("</p><p>");
document.write("<p><a href='");
document.write(x[i].getElementsByTagName("url")[0].childNodes[0].nodeValue);
}
document.write("'>Click here to go through to the LinkedIn group</a></p>");
相关问题