[
Lists Home |
Date Index |
Thread Index
]
Many of the modern XSLT processors have a simple way to get parameters
in and out of a transformation. An example using PHP and the libxslt
processor (works pretty much the same way with Java and Xalan):
// Create a new DOM object and load it from an XSL file
$xsl = new DomDocument();
$xsl->load('stylesheet.xsl');
// Create a new DOM object and load it from an XML file
$source = new DomDocument();
$source->load('source.xml');
// Create a new Processor and import the XSL stylesheet
$transform = new XSLTProcessor ();
$transform->importStyleSheet($xsl);
// Set the input parameters (namespace, param-name, value)
$param_input = "TagToMatch";
$transform->setParameter( '', 'param_input', $param_input );
// Transform the document
$result = $transform->transformToDoc($source);
// Retrieve the output parameters
$output = $transform->getParameter( '', 'parameter_output');
Given the above code (or something similar in another language), I am
then able to use the input parameter value in the XSL, as long as the
<xsl:param> element is defined as a top-level element:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="param_input"/>
<xsl:template match="/">
<xsl:copy-of select="dyn:evaluate($param_input)"/>
</xsl:template>
</xsl:stylesheet>
Where I run into problems is trying to get a parameter back out of the
XSL. I know that there is some method to do this since the parser
implements a functioning getParameter() method, but how do I create a
top-level <xsl:param> element and assign it a value somewhere later down
the parse tree? This functionality is fairly important to a project I
am working on, so any help would be greatly appreciated.
|