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

时间:2012-05-17 10:03:44

标签: xslt xslt-1.0

我知道这是一个老问题已经传了好几次但是我想知道是否有人可以扩展是否可以通过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 / p>

e.g。 <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 个答案:

答案 0 :(得分:2)

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

  1. <xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/> 我怀疑所需的样式表可以直接从其SourceForge视图页面导入 - 特别是考虑到它本身会导入其他FXSL样式表。使用FXSL的正确方法是将其下载到本地计算机,并从其驻留在本地计算机上的文件位置引用其样式表。
  2. ...

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

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

    以下是您想要的代码示例(前提是您已经下载了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>
    

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

    param1 : a
    param2 : b