É possível passar um parâmetro para XSLT através de uma URL ao usar um navegador para transformar XML?
Pergunta
Ao utilizar um navegador para transformar XML (Google Chrome ou IE7) é possível passar um parâmetro para a folha de estilo XSLT através da URL?
exemplo:
dados.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>
amostra.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>
Solução
Você pode gerar o XSLT no lado do servidor, mesmo que a transformação seja no lado do cliente.
Isso permite que você use um script dinâmico para manipular o parâmetro.
Por exemplo, você pode especificar:
<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?>
E então, em myscript.cfm você geraria o arquivo XSL, mas com o script dinâmico manipulando o parâmetro da string de consulta (isso varia dependendo da linguagem de script usada).
Outras dicas
Infelizmente, não - você não pode passar parâmetros para o XSLT apenas no lado do cliente.O navegador pega as instruções de processamento do XML;e o transforma diretamente com o XSLT.
É possível passar valores por meio da URL da querystring e depois lê-los dinamicamente usando JavaScript.No entanto, estes não estariam disponíveis para uso no XSLT (expressões XPath) - pois o navegador já transformou o XML/XSLT.Eles só poderiam ser usados na saída HTML renderizada.
Basta adicionar o parâmetro como um atributo ao arquivo de origem XML e usá-lo como um atributo com a folha de estilo.
xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam"))
E a função JavaScript é a seguinte:
//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";
}
}