- PR -

XSLにJavaScriptを埋め込みページを動的に操作する

1
投稿者投稿内容
hiro
会議室デビュー日: 2004/06/18
投稿数: 9
投稿日時: 2004-06-18 14:30
こんにちは。
XSLにJavaScriptを埋め込みページを操作しようと思ったのですがうまく動きません。
とりあえず、window.alertだけでもと思うのですが、お力を貸してください。
このXSLの狙いは、チェックされたラジオボタンによってwindow.alertの表示をを変えたいのですが、
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" encoding="Shift_JIS" />

<xsl:template match="/">
<html>
<head>
<script language="JavaScript">
<![CDATA[
function check(){
window.alert("hello!");
if(document.form1.answer[0].checked == true){
window.alert("間違いです。");
}
if(document.form1.answer[1].checked == true){
window.alert("正解です。");
}
if(document.form1.answer[2].checked == true){
window.alert("微妙です。。");
}
if(document.form1.answer[3].checked == true){
window.alert("不思議です。");
}
elese{window.alert("ダメだこりゃ");}
}
]]>
</script>

</head>
<body>
<table border="0">
<xsl:apply-templates select="Books"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Books">
<xsl:for-each select="Question">
<tr>
<td>Look at the below.</td>
</tr>
<tr>
<td rowspan="2">
<xsl:element name="img">
<xsl:attribute name="height">
150
</xsl:attribute>
<xsl:attribute name="width">
250
</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="Picture" />
</xsl:attribute>
</xsl:element>
</td>
<xsl:element name="td">
<xsl:attribute name="colspan">4</xsl:attribute>
<xsl:attribute name="valign">bottom</xsl:attribute>
Now listen to the four statements.
</xsl:element>
<xsl:element name="td">
<xsl:attribute name="colspan">2</xsl:attribute>
<xsl:attribute name="valign">bottom</xsl:attribute>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="Voice" />
</xsl:attribute>
<img border="0" src="voice.gif" />
</xsl:element>
</xsl:element>
</tr>
<tr>
<form name="form1">
<xsl:element name="input">
<xsl:attribute name="type">hidden</xsl:attribute>
<xsl:attribute name="name">title</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="@title" /></xsl:attribute>
</xsl:element>
<td>
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">answer</xsl:attribute>
<xsl:attribute name="value">A</xsl:attribute>
(A)
</xsl:element>
</td>
<td>
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">answer</xsl:attribute>
<xsl:attribute name="value">B</xsl:attribute>
(B)
</xsl:element>
</td>
<td>
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">answer</xsl:attribute>
<xsl:attribute name="value">C</xsl:attribute>
(C)
</xsl:element>
</td>
<td>
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">answer</xsl:attribute>
<xsl:attribute name="value">D</xsl:attribute>
(D)
</xsl:element>
</td>
<td>
<xsl:element name="input">
<xsl:attribute name="type">button</xsl:attribute>
<xsl:attribute name="name">true</xsl:attribute>
<xsl:attribute name="value">正誤</xsl:attribute>
<xsl:attribute name="onClick">check()</xsl:attribute>
</xsl:element>
</td>
<td>
<xsl:element name="input">
<xsl:attribute name="type">submit</xsl:attribute>
<xsl:attribute name="name">explain</xsl:attribute>
<xsl:attribute name="value">解説</xsl:attribute>
</xsl:element>
</td>
</form>
</tr>
<tr>
<td colspan="7"><img src="line3.gif" /></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
fofofo
会議室デビュー日: 2003/01/06
投稿数: 8
お住まい・勤務地: 大阪
投稿日時: 2004-06-20 11:23
こんにちは。
JavaScriptのelse句のスペルが間違っているのが原因では…

私の環境で下記のXMLを用意して、XSLTのJavaScript部分を直したら、
alertは表示されましたけど。。。(ちなみにMS-XML4.0です)

<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="Test.xsl"?>
<Books>
<Question title="test">
<Picture>hoge.gif</Picture>
<Voice>hoge.html</Voice>
</Question>
</Books>


あと、XSLTの質問をされる時は、変換元のXMLも書いた方が
レスがつきやすいと思いますよ。
hiro
会議室デビュー日: 2004/06/18
投稿数: 9
投稿日時: 2004-06-22 17:51
すみません(汗)
elseを直したら動作しました。
お手数をおかけしました。
ありがとうございます。
1

スキルアップ/キャリアアップ(JOB@IT)