上のリクエストで得られたXMLからタイトル一覧を作る:
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:a="http://www.w3.org/2005/Atom">
<xsl:output method="html" encoding="Shift_JIS"/>
<xsl:template match="/a:feed">
<ol>
<xsl:apply-templates select="a:entry"/>
</ol>
</xsl:template>
<xsl:template match="a:entry">
<li>
<xsl:value-of select="a:title"/>
</li>
</xsl:template>
</xsl:stylesheet>
デフォルトのnamespaceはAtomなので、てきとうにxmlns:aとかつけとく。
大体同じことをするJavaScript:
<html>
<head>
<script type="text/javascript">
load_data = function(q){
var opt = "vq=" + q + "&orderby=published&start-index=1&max-results=50";
var i;
var xdoc = new ActiveXObject("Microsoft.XMLDOM");
xdoc.async = false;
xdoc.load("http://gdata.youtube.com/feeds/api/videos?" + opt);
var nodes = xdoc.getElementsByTagName("feed/entry/title");
document.form1.result.value = "";
for(i = 0; i < nodes.length; i++){
document.form1.result.value += "** " + nodes[i].firstChild.nodeValue + "\n\n";
}
}
</script>
</head>
<body>
<form name="form1" onSubmit="return false">
<p>
query: <input type="text" name="query" size="35" value="melty">
<input type="button" value="Run" onclick="load_data(document.form1.query.value)">
</p>
<textarea name="result" rows="15" cols="80"></textarea>
</form>
</body>
</html>
最終更新:2009年12月07日 04:00