查询参数操作



我有一个URL,其中查询参数需要从中选择一个特定的值。

例如:/abc/xyz?gcode=123456&pcode=21314u925

需要选择此值 (gcode=123456(我使用了如下条件

<xsl:value-of select="substring-before(substring-after($currentURI,'?gcode='),'&amp;pcode=')"/>

问题是在 URI 中,查询参数的顺序并不总是 gcode 后跟 pcode。 在 gcode 之后,它可以有任何其他参数。

我怎样才能获得该值

我的第一个想法是你应该尝试实现该序列或类似的东西:

  • 获取字符串您的参数(您可以在"/abc/xyz?
  • 使用标记化函数(获取参数列表(
  • 获取包含 gcode 的列表元素

在这里,您可以获得一些如何在xslt 1.0或xslt 2.0中使用tokenize的示例

我希望这可以帮助您解决问题

你的尝试很接近。我要做的是:

  • ?gcode=中删除?
  • &amp;pcode=更改为仅&amp;
  • 添加一个 concat 以添加尾随&amp;以防万一gcode是最后一个查询参数

例。。。

<xsl:value-of 
 select="substring-before(concat(substring-after($currentURI,'gcode='),'&amp;'),'&amp;')"/>
如果需要

多次获取查询参数的值,也可以使用命名模板 (XSLT 1.0+( 或 xsl:function (XSLT 2.0+(。

例子。。。

XML 输入

<tests>
    <test><![CDATA[/abc/xyz?gcode=123456&pcode=21314u925]]></test>
    <test><![CDATA[/abc/xyz?pcode=21314u925&gcode=123456]]></test>
    <test>/abc/xyz?gcode=123456&amp;pcode=21314u925</test>
    <test>/abc/xyz?pcode=21314u925&amp;gcode=123456</test>
</tests>

XSLT 1.0 (小提琴: http://xsltfiddle.liberty-development.net/b4GWVm(

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="test">
    <xsl:variable name="currentURI" select="."/>
    <gcode>
      <xsl:call-template name="get-param">
        <xsl:with-param name="name" select="'gcode'"/>
        <xsl:with-param name="uri" select="$currentURI"/>
      </xsl:call-template>
    </gcode>
  </xsl:template>
  <xsl:template name="get-param">
    <xsl:param name="name"/>
    <xsl:param name="uri"/>
    <xsl:value-of 
      select="substring-before(concat(
      substring-after($uri, concat($name,'=')),'&amp;'),
      '&amp;')"/>    
  </xsl:template>
</xsl:stylesheet>

XSLT 2.0 (小提琴: http://xsltfiddle.liberty-development.net/b4GWVm/1(

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:l="local" exclude-result-prefixes="l xs">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:function name="l:get-param">
    <xsl:param name="name" as="xs:string"/>
    <xsl:param name="uri" as="xs:string"/>
    <xsl:value-of 
      select="substring-before(concat(
      substring-after($uri,concat($name,'=')),'&amp;'),
      '&amp;')"/>    
  </xsl:function>
  <xsl:template match="test">
    <xsl:variable name="currentURI" select="."/>
    <gcode>
      <xsl:value-of select="l:get-param('gcode',$currentURI)"/>
    </gcode>
  </xsl:template>
</xsl:stylesheet>

XML 输出(来自上面的任一样式表(

<tests>
   <gcode>123456</gcode>
   <gcode>123456</gcode>
   <gcode>123456</gcode>
   <gcode>123456</gcode>
</tests>

XSLT-1.0 解决方案可以使用xsl:choose来处理这两种可能性:

  1. 另一个参数如下
  2. gcode= 是字符串中的最后一个参数

所以一些代码可能看起来像这样:

<xsl:variable name="tmp" select="substring-after($currentURI,'gcode=')" />
<xsl:variable name="GCODE">
    <xsl:choose>
        <xsl:when test="contains($tmp,'&amp;')">  <!-- other parameters do follow -->
            <xsl:value-of select="substring-before($tmp,'&amp;')" />
        </xsl:when>
        <xsl:otherwise>                           <!-- last parameter -->
            <xsl:value-of select="$tmp" />   
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:value-of select="$GCODE" />                  <!-- Value of parameter = '123456' -->

最新更新