我知道这是一个已经经历了这么多次通过一个老问题,但我不知道是否有人能扩张是否具有附加给它的查询字符串的URL可以通过XSLT 1.0被剥离出来,可以用来作为以后参数使用XSLT转换。
例如,我有一个URL http://www.mydomain.com/mypage.htm?param1=a¶m2=b
通过XSLT,我要寻找的沿着线的东西造成的:
<xsl:param name="param1">a</xsl:param>
和<xsl:param name="param2">b</xsl:param>
其中两个参数名(参数1,参数2),它的值(A,B)已经从quesrystring提取允许我使用$param1
和$param2
在if条件以后再说
如<xsl:if test="$param1 = 'a'>
出来的真实的,但如果我们使用<xsl:if test="$param1 = 'b'>
出来假的。
我在这里看到了类似的问题: 检索页面网址参数或在XSLT页面URL ,它使用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="'?&'"/>
</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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
有代码中的几个问题:
-
<xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/>
我怀疑有用样式表可以直接从被导入其SourceForge上查看页面 -尤其是考虑到,它本身导入其他FXSL样式表。 使用FXSL正确的方法是将其下载到本地计算机,并引用其样式关闭它在本地计算机驻留在文件中的位置。
...
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&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="'?&'"/>
</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>
</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