2012-09-19 27 views
0

我有一個具有某種URL例如http://www.example.xml 打開一個XSL-XML頁面有時URL具有例如http://www.example.xml?pLanguage=nl&source=enews把URL參數轉換成一個XSL變量

我想提取從源參數一些參數 該URL並將其放入xsl:變量中。我認爲這樣做與JavaScript與window.top.location.search.substring(1);我想這樣做與JavaScript的window.top.location.search.substring(1);把我不知道如何把結果放入一個xsl:變量。

有人可以幫忙嗎?

回答

0

XSLT處理器通常提供一種設置來設置樣式表中全局參數的值。例如,XSLTProcessor in Gecko

這是一個應該在基於Gecko的瀏覽器上工作的例子(例如FireFox)。

首先,你需要一個帶有xsl:param名稱的樣式表。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<!-- file: /xslt-stylesheet.xsl --> 
<xsl:param name="pLanguage"></xsl:param> 
<xsl:param name="source"></xsl:param> 

<xsl:template match="/"> 
    <root> 
    <pLanguage><xsl:value-of select="$pLanguage"/></pLanguage> 
    <source><xsl:value-of select="$source"/></source> 
    </root> 
</xsl:template> 

</xsl:stylesheet> 

然後把這個JavaScript在HTML文件的瀏覽器來解釋:

// Var to hold the transformed document 
var transformeddoc = null; 
// source document to transform. We'll use document.body as an easy example. 
var sourcedoc = document.body; 
// extract parameters from the window's query string 
var params = extractparams(window.location.search); 
// create an xslt processor 
var xsltproc = new XSLTProcessor(); 
// set stylesheet paramaters 
xsltproc.setParameter(null, 'pLanguage', params.pLanguage); 
xsltproc.setParameter(null, 'source', params.source); 
// load xslt document 
var xslt = document.implementation.createDocument("", "test", null); 
xslt.addEventListener("load", processXslt, false); 
xslt.load("/xslt-stylesheet.xsl"); 

// after document is loaded, perform the transform 
function processXslt() { 
    xsltproc.importStylesheet(xslt); 
    transformeddoc = xsltproc.transformToDocument(sourcedoc); 
    // transformeddoc is now a DOM of this xml: 
    // <root><pLanguage>pl</pLanguage><source>sc</source></root> 
} 

function extractparams(querystring) { 
    var params = {'pLanguage':'', 'source':''}; 
    var qs = querystring.replace(/^\?/, ''); 
    qs = qs.split('&'); 
    for (var i=0, kv; i<qs.length; i++) { 
     kv = qs[i].split('='); 
     if (kv.length===2 && kv[0] in params) { 
      params[kv[0]] = kv[1]; 
     } 
    } 
    return params; 
} 
+0

通過參數我沒有意思的xsl:PARAM。我的意思是我的意思是URL的參數。在http://www.example.xml中?pLanguage = nl&source = enews我的意思是參數pLanguage和source。 – Bigjo

+0

我已經用一個例子擴大了答案,但同樣的原則適用於「將結果放入xsl:變量」。 –

相關問題