首页 文章

XSLT:将URL查询字符串作为参数传递

提问于
浏览
2

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

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

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

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

其中参数名称(param1,param2)和它的值(a,b)都是从quesrystring中提取出来的,以便我可以在if条件下使用 $param1$param2

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

我在这里看到了一个类似的问题:Retrieve page URL params or page URL in XSLT使用了 str-split-to-words 模板,但是我没有成功地使它工作(可能是由于我以错误的方式实现它)所以任何有关如何在实践中完成它的工作示例将是非常有益的 .

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 回答

  • 2

    您的代码中存在一些问题:

    • <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 - 特别是考虑到它本身会导入其他FXSL样式表 . 使用FXSL的正确方法是将其下载到本地计算机,并从其驻留在本地计算机上的文件位置引用其样式表 .

    ...

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

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

    Here is an example of the code you want (假设您已下载FXSL,解压缩分发并将此代码保存在与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>
    

    When this transformation is applied on any XML document (not used in this demo), the wanted, correct result -- the query-string parameters referenced of a results variable by name -- is produced:

    param1 : a
    param2 : b
    

相关问题