问题
I have an XML on which I am displaying the test cases as HTML using XSLT.
I have one query.
How to pass a variable as a parameter and access it on the other page.
So far I achieved this:
<xsl:for-each select="//test-suite[@type='TestFixture']">
<tr>
<xsl:choose>
<xsl:when test="contains(@result, 'Success')">
<xsl:variable name="nameOfPage" select="@name" />
<td><a href="DownloadManagerFeature.xml?nameOfPage={$nameOfPage}" style="text-decoration: none" ><font color="239625"><xsl:value-of select="@name"/></font></a></td>
<td><xsl:value-of select="@result"/></td>
<td><xsl:value-of select="@time"/></td>
</xsl:when>
As you can see, I am setting the value of name parameter to nameOfPage and
I am passing it as a URL parameter.
But I am facing some issues retrieving it on other page.
<td>test page</td>
<td><xsl:value-of select="@nameOfPage"/></td>
The value is coming as null. I even added this at the top:
<xsl:variable name="nameOfPage" select="document('Mimedrive.Tests.xml')"/>
I am trying to match the nameOfPage to the @name in the case. When I do it with hardcoded values its working , like:
</tr>
<xsl:for-each select="//test-case">
<xsl:if test="contains(@name, 'XXX.DownloadManagerFeature')">
<tr>
<td><xsl:value-of select="@description"/></td>
<td><xsl:value-of select="@result"/></td>
</tr>
Please help.
回答1:
Use the JavaScript binding of XSLT to set the nameOfPage parameter before processing.
Assuming the XML document has the XHTML namespace as the default, then:
function transform()
{
var nameOfPage = var value=RegExp("nameOfPage[^&]+").exec(window.location.search)[0].replace(/[^=]+./,"");
with (new XSLTProcessor)
{
setParameter(null, "nameOfPage", nameOfPage);
importStylesheet("foo.xsl");
transform.result = transformToFragment(this, document);
transform.root = transform.result.xml;
document.getElementById(appendTo).appendChild(transform.root);
}
}
Or use the processor specific processing instruction, as in Firefox:
<?xslt-param name="nameOfPage" value="foo"?>
<?xml-stylesheet type="text/xsl" href="foo.xsl"?>
Or use the processor specific setParameter method, for instance PHP:
$transformer = new XSLTProcessor();
$transformer->importStylesheet("foo.xsl");
$transformer->setParameter('', 'nameOfPage', $_POST['nameOfPage']);
References
- PI Parameters
- Setting Parameters for XSLTProcessor
- XSLTProcessor::setParameter
- Saxon:Running a Transformation from the command line
- Saxon:setParams
- How to Write CGI Applications in Visual Basic
来源:https://stackoverflow.com/questions/23356113/passing-and-accessing-parameters-in-xslt