ここで私の質問のフォローアップとして - How to transform an XML file with XSLT, using a Greasemonkey script? - 別の問題に直面しています:
いくつかの div の表示を制御するために、XSL テンプレートでいくつかの基本的な JavaScript 関数を使用したいと考えています。ただし、これらの JavaScript 関数をどのように含めても、認識されないようです。私はすでに多くのことを調査しましたが、それを回避することはできません。
私は2つのことを試しました:
<script>
タグ内の XSL テンプレートに JavaScript を追加する<script>
Greasemonkey スクリプト自体に新しいタグを追加する
jQuery や外部 JS ファイル (私も試しました) をできるだけシンプルに保つために使用したくないのですが、それで問題が解決する場合は、すべてを変更することができます!
どちらの場合でも、関数を呼び出すと、ReferenceError: x is not defined
. ただし、JavaScript コードが最終的な HTML 結果にうまく収まっていることがわかります。Firebug を使用<script>
して、プレーンな html ページに「こんにちは」と警告する単純な機能を持つ新しいタグを追加すると、完全に機能します。これが XSLT 変換の上で実行された場合にのみ、問題が発生します (簡単にするために、単純な関数を使用して警告ボックスを表示しています)。
ここに私のサンプルデータがあります:
<?xml version="1.0" encoding="utf-8"?>
<Results>
<Result>
<Listings total="2">
<Res>
<Result index="0">
<id>123456</id>
<name>My Business</name>
<category>Restaurants</category>
<phone>9872365</phone>
</Result>
</Res>
<Res>
<Result index="1">
<id>876553</id>
<name>Some Other Business</name>
<category>Restaurants</category>
<phone>9834756</phone>
</Result>
</Res>
</Listings>
</Result>
</Results>
<script>
タグにタグを追加した最初の試みは次の<head>
とおりです。
// ==UserScript==
// @name _Test XML Renderer
// @description stylesheet for xml results
// @include *
// @grant none
// ==/UserScript==
var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
<xsl:output method="html"/>\n\
<xsl:template match="/">\n\
<html>\n\
<head><script type="text/javascript">function hello() {alert("hello")};</script></head>\n\
<body>\n\
<table id="results" border="1" cellspacing="0" cellpadding="0">\n\
<thead>\n\
<tr>\n\
<th class="name">id</th>\n\
<th class="name">category ID</th>\n\
<th class="name">name</th>\n\
<th class="name">phone</th>\n\
</tr>\n\
</thead>\n\
<tbody>\n\
<xsl:for-each select="Results/Result/Listings/Res">\n\
<tr>\n\
<td class="small" width="120">\n\
<a href="#" onclick="hello()"><xsl:value-of select="Result/id"/></a>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/category"/>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/name"/>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/phone"/>\n\
</td>\n\
</tr>\n\
</xsl:for-each>\n\
</tbody>\n\
</table>\n\
</body>\n\
</html>\n\
</xsl:template>\n\
</xsl:stylesheet>\n\
';
var processor = new XSLTProcessor ();
var dataXSL = new DOMParser ().parseFromString (xsl_str, "text/xml");
processor.importStylesheet (dataXSL);
var newDoc = processor.transformToDocument (document);
//-- These next lines swap the new, processed doc in for the old one...
window.content = newDoc;
document.replaceChild (
document.importNode (newDoc.documentElement, true),
document.documentElement
);
XSL テンプレートの外側に「hello」関数を追加する別の試みを次に示します。
// ==UserScript==
// @name _Test XML Renderer
// @description stylesheet for xml results
// @include *
// @grant none
// ==/UserScript==
var xsl_str = '<?xml version="1.0" encoding="utf-8"?>\n\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">\n\
<xsl:output method="html"/>\n\
<xsl:template match="/">\n\
<html>\n\
<head></head>\n\
<body>\n\
<table id="results" border="1" cellspacing="0" cellpadding="0">\n\
<thead>\n\
<tr>\n\
<th class="name">id</th>\n\
<th class="name">category ID</th>\n\
<th class="name">name</th>\n\
<th class="name">phone</th>\n\
</tr>\n\
</thead>\n\
<tbody>\n\
<xsl:for-each select="Results/Result/Listings/Res">\n\
<tr>\n\
<td class="small" width="120">\n\
<a href="#" onclick="hello()"><xsl:value-of select="Result/id"/></a>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/category"/>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/name"/>\n\
</td>\n\
<td class="small" width="120">\n\
<xsl:value-of select="Result/phone"/>\n\
</td>\n\
</tr>\n\
</xsl:for-each>\n\
</tbody>\n\
</table>\n\
</body>\n\
</html>\n\
</xsl:template>\n\
</xsl:stylesheet>\n\
';
var processor = new XSLTProcessor ();
var dataXSL = new DOMParser ().parseFromString (xsl_str, "text/xml");
processor.importStylesheet (dataXSL);
var newDoc = processor.transformToDocument (document);
var script = "function hello() {alert('hello')};";
var newElem = newDoc.createElement('script');
newElem.type = 'text/javascript';
newElem.appendChild(newDoc.createTextNode(script));
newDoc.getElementsByTagName('head').item(0).appendChild(newElem);
//-- These next lines swap the new, processed doc in for the old one...
window.content = newDoc;
document.replaceChild (
document.importNode (newDoc.documentElement, true),
document.documentElement
);