2008-09-15 27 views
14

ブラウザを使用してXML(Google ChromeまたはIE7)を変換すると、URLを介してパラメータをXSLTスタイルシートに渡すことはできますか?ブラウザを使用してXMLを変換するときに、URLを介してパラメータをXSLTに渡すことはできますか?

例:

data.xmlに

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="sample.xsl"?> 
<root> 
    <document type="resume"> 
     <author>John Doe</author> 
    </document> 
    <document type="novella"> 
     <author>Jane Doe</author> 
    </document> 
</root> 

sample.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:output method="html" /> 
    <xsl:template match="/"> 
    <xsl:param name="doctype" /> 
    <html> 
     <head> 
      <title>List of <xsl:value-of select="$doctype" /></title> 
     </head> 
     <body> 
      <xsl:for-each select="//document[@type = $doctype]"> 
       <p><xsl:value-of select="author" /></p> 
      </xsl:for-each> 
     </body> 
    </html> 
</<xsl:stylesheet> 

答えて

3

あなたは変換がクライアント - であっても、XSLTサーバー側を生成することができます側。

これにより、ダイナミックスクリプトを使用してパラメータを処理できます。

たとえば、次のように指定があります

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?> 

をそしてmyscript.cfmに、あなたは、出力XSLファイルは、しかし、動的スクリプトは、クエリ文字列パラメータを処理して(これは変化するであろうと思われるスクリプト言語あなたに応じて、つかいます)。

+4

の部分は、それが唯一の可能なクライアントサイドのですか? – user9547

6

残念ながら、クライアントサイドでのみパラメータをXSLTに渡すことはできません。 WebブラウザはXMLから処理指示を受け取ります。それをXSLTで直接変換します。


クエリ文字列URLを介して値を渡し、JavaScriptを使用して動的に読み取ることができます。しかし、ブラウザはすでにXML/XSLTを変換しているため、これらはXSLT(XPath式)で使用することはできません。レンダリングされたHTML出力でのみ使用できます。

6

パラメータを属性としてXMLソースファイルに追加し、それをスタイルシートでアトリビュートとして使用するだけです。

xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam")) 

そしてJavaScript関数は次のとおりです。質問の2は次のようになり

//Get querystring request paramter in javascript 
function getParameter (parameterName) { 

    var queryString = window.top.location.search.substring(1); 

    // Add "=" to the parameter name (i.e. parameterName=value) 
    var parameterName = parameterName + "="; 
    if (queryString.length > 0) { 
     // Find the beginning of the string 
     begin = queryString.indexOf (parameterName); 
     // If the parameter name is not found, skip it, otherwise return the value 
     if (begin != -1) { 
     // Add the length (integer) to the beginning 
     begin += parameterName.length; 
     // Multiple parameters are separated by the "&" sign 
     end = queryString.indexOf ("&" , begin); 
     if (end == -1) { 
     end = queryString.length 
     } 
     // Return the string 
     return unescape (queryString.substring (begin, end)); 
    } 
    // Return "null" if no parameter has been found 
    return "null"; 
    } 
} 
関連する問題