XSLT:将 URL 查询字符串作为参数传递 [英] XSLT: Passing URL querystring as a parameter

查看:27
本文介绍了XSLT:将 URL 查询字符串作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个已经被多次传递的老问题,但我想知道是否有人可以扩展附加了查询字符串的 URL 是否可以通过 XSLT 1.0 剥离并可以用作供以后使用 XSLT 转换的参数.

I know this is an old question that has been passed around SO several times but I was wondering whether anyone can expand on whether a URL that has a querystring attached to it can be stripped out via XSLT 1.0 and can be used as a parameter for later use of the XSLT transformation.

例如,我有一个网址 http://www.mydomain.com/mypage.htm?param1=a&param2=b

通过 XSLT,我正在寻找类似以下内容的结果:

via XSLT, I am looking for a result of something along the lines of:

<xsl:param name="param1">a</xsl:param>

从查询字符串中提取参数名称 (param1, param2) 及其值 (a, b) 以允许我使用 $param1$param2稍后在 if 条件下说

where both parameter name (param1, param2) and it's value (a, b) has been extracted from the quesrystring to allow me to use $param1 and $param2 later on say in an if condition

例如<xsl:if test="$param1 = 'a'> 结果为真,但如果我们使用 <xsl:if test="$param1 = 'b'> 结果是假的.

e.g. <xsl:if test="$param1 = 'a'> comes out true but if we use <xsl:if test="$param1 = 'b'> comes out false.

我在这里看到了一个类似的问题:Retrieve page URLparams 或 XSLT 中的页面 URL 使用 str-split-to-words 模板,但我没有成功地让它工作(可能是因为我以错误的方式实现它)所以任何工作示例如何在实践中做到这一点将非常有益.

I have seen a similar question here: Retrieve page URL params or page URL in XSLT which uses the str-split-to-words template but I have unsuccessfully got it working (possibly due to me implementing it the wrong way) so any working examples of how it can be done in practice would be massively beneficial.

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common">
<xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/>
<xsl:output indent="yes" method="html"/>

<xsl:template match="/">
<xsl:variable name="vwordNodes">
  <xsl:call-template name="str-split-to-words">
    <xsl:with-param name="pStr" select="$pQString"/>
    <xsl:with-param name="pDelimiters" select="'?&amp;'"/>
  </xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
</xsl:template>

<xsl:template match="word">
  <xsl:value-of select="."/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

</xsl:stylesheet>

推荐答案

你的代码有几个问题:

  1. <xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/> 我怀疑是否可以直接从其 SourceForge 视图页面导入所需的样式表 -- 特别是考虑到它本身会导入其他 FXSL 样式表.使用 FXSL 的正确方法是将其下载到本地计算机,并从它驻留在本地计算机上的文件位置引用其样式表.
  1. <xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/> I doubt that the wanted stylesheet can be imported directly from its SourceForge view page -- especially taking into account, that it itself imports other FXSL stylesheets. The correct way to use FXSL is to download it to the local computer and reference its stylesheets off the file location it resides in at the local computer.

...

.2.<xsl:with-param name="pStr" select="$pQString"/> 这将产生编译错误,因为您错过了定义 $pQString 全局/外部参数.您需要在全局级别定义此参数.可以给它一个默认值(例如一个特定的 URL)以便于测试.然而,使用这个参数的想法是转换的调用者应该把这个参数传递给转换.

.2. <xsl:with-param name="pStr" select="$pQString"/> This will produce a compilation error because you missed to define the $pQString global/external parameter. You need to define this parameter at global level. It can be given a default value (for example a particular URL) for easier testing. However, the idea of using this parameter is that the invoker of the transformation should pass this parameter to the transformation.

.3.转换的结果写入输出.虽然这有利于演示目的,但您希望能够在稍后的转换中使用这些结果.这样做的方法是将这些结果捕获到一个变量中,从中生成另一个变量,使用常规树(来自其 RTF 类型),然后引用最后一个变量的节点.

.3. The results of the transformation are written to the output. While this is good for demonstration purposes, you want to be able to use these results later in the transformation. The way to do this is to capture these results in a variable, make another variable from it, with a regular tree (from its RTF type) and then reference the nodes of this last variable.

这是你想要的代码示例(前提是你已经下载了 FXSL,解压了分发包并将这段代码保存在与 FXSL 解压分发包相同的目录中):

Here is an example of the code you want (provided that you have downloaded FXSL, unzipped the distribution and saved this code in the same directory as the unzipped distribution of FXSL):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 >

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>

   <xsl:param name="pUrl" select=
   "'http://www.mydomain.com/mypage.htm?param1=a&amp;param2=b'"/>

   <xsl:param name="pQString" select=
     "substring-after($pUrl, '?')"
     />


    <xsl:template match="/">
        <xsl:variable name="vwordNodes">
          <xsl:call-template name="str-split-to-words">
            <xsl:with-param name="pStr" select="$pQString"/>
            <xsl:with-param name="pDelimiters"
                      select="'?&amp;'"/>
          </xsl:call-template>
        </xsl:variable>

       <xsl:variable name="vrtfqueryParams">
         <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
       </xsl:variable>

       <xsl:variable name="vqueryParams" select="ext:node-set($vrtfqueryParams)/*"/>

       <xsl:value-of select="$vqueryParams/@name[. ='param1']"/>
       <xsl:text> : </xsl:text>
       <xsl:value-of select="$vqueryParams[@name = 'param1']"/>

       <xsl:text>&#xA;</xsl:text>
       <xsl:value-of select="$vqueryParams/@name[. ='param2']"/>
       <xsl:text> : </xsl:text>
       <xsl:value-of select="$vqueryParams[@name = 'param2']"/>
    </xsl:template>

    <xsl:template match="word">
      <param name="{substring-before(.,'=')}">
        <xsl:value-of select="substring-after(.,'=')"/>
      </param>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于任何 XML 文档(本演示中未使用)时,将生成所需的正确结果 -- 按名称引用的结果变量的查询字符串参数:

param1 : a
param2 : b

这篇关于XSLT:将 URL 查询字符串作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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