在XSLT中传递和访问参数 [英] Passing and accessing parameters in XSLT

查看:207
本文介绍了在XSLT中传递和访问参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML,正在使用XSLT将测试用例显示为HTML.

I have an XML on which I am displaying the test cases as HTML using XSLT.

我有一个查询.

如何将变量作为参数传递并在另一页上访问它.

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>

如您所见,我将name参数的值设置为nameOfPage, 我将其作为URL参数传递.

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')"/>

在这种情况下,我试图将nameOfPage与@name匹配. 当我使用硬编码值进行工作时,例如:

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>

请帮助.

推荐答案

在处理之前,请使用XSLT的JavaScript绑定设置nameOfPage参数.

Use the JavaScript binding of XSLT to set the nameOfPage parameter before processing.

假设XML文档的默认名称为XHTML命名空间,则:

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);
     }
  }

或使用特定于处理器的处理指令,如Firefox中的

Or use the processor specific processing instruction, as in Firefox:

<?xslt-param name="nameOfPage" value="foo"?>
<?xml-stylesheet type="text/xsl" href="foo.xsl"?>

或使用处理器特定的setParameter方法,例如PHP:

Or use the processor specific setParameter method, for instance PHP:

$transformer  = new XSLTProcessor();
$transformer->importStylesheet("foo.xsl");
$transformer->setParameter('', 'nameOfPage', $_POST['nameOfPage']);

参考

  • 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

这篇关于在XSLT中传递和访问参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆