使用 xslt 的查询字符串 [英] querystring using xslt

查看:26
本文介绍了使用 xslt 的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 xslt 访问查询字符串?

我有一个网址,例如

www.example.com/page.aspx?k=aa&lang=en

我想做类似的事情

if lang = en

显示内容

其他

 

显示其他内容

你能告诉我如何使用 xslt 做到这一点吗?

解决方案

是否可以访问查询字符串使用 xslt?

是,如果查询字符串作为参数传递.

下面的代码表明访问查询字符串不需要扩展函数.它可以作为(全局)参数传递.这是首选,因为它减少了对扩展的需求,并使代码更清晰、更易读.

然后可以执行标记化(使用 tokenize() 函数在 XSLT 2.0 或 XSLT 1.0 中使用 str-split-to-words FXSL 1.x 或自己编写的递归标记化模板.)

XSLT 1.0 解决方案:

<xsl:value-of select="concat('lang = ', $vLang)"/></xsl:模板></xsl:stylesheet>

当上述转换应用于任何 XML 文档(不会被使用)时,就会产生想要的结果:

lang = en

注意 使用 FXSL 1.x str-split-to-words 模板和使用 EXSLT ext:node-set() 扩展函数.

XSLT 2.0 解决方案:

<xsl:output indent="yes" omit-xml-declaration="yes"/><xsl:param name="pQString" as="xs:string" select='?login=userId&amp;tag=XSLT&amp;lang=en&amp;level=expert'"/><xsl:template match="/"><xsl:variable name="vLang" as="xs:string" select=子串后(tokenize($pQString, '\?|&amp;')[开始于(.,'lang=')][最后的()],朗=")"/>lang = "<xsl:sequence select='$vLang'/>"</xsl:模板></xsl:stylesheet>

执行上述 XSLT 2.0 转换时,会产生正确的结果:

 lang = "en"

is it possible to access a query string using xslt?

i have a url e.g

www.example.com/page.aspx?k=aa&lang=en

I want to do something like

if lang = en

<div>displaly stuff</div>

else

 <div>display other stuff</div>

can you show me how to do this using xslt?

解决方案

is it possible to access a query string using xslt?

Yes, if the query string is passed as a parameter.

The code below shows that no extension function is required to access a query-string. It can be passed as a (global) parameter. This is to be preferred as it reduces the need for extensions and results in cleaner and more readable code.

Then one can perform tokenization (with the tokenize() function in XSLT 2.0 or in XSLT 1.0 using the str-split-to-words template of FXSL 1.x or a self-written recursive tokenization template.)

XSLT 1.0 solution:

<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="pQString" select=
     "'?login=userId&amp;tag=XSLT&amp;lang=en&amp;level=expert'"
     />


    <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="vLang" select=
      "substring-after(ext:node-set($vwordNodes)/*
                             [starts-with(.,'lang=')]
                               [last()],
                       'lang='
                      )
      "/>

      <xsl:value-of select="concat('lang = ', $vLang)"/>
    </xsl:template>
</xsl:stylesheet>

when the above transformation is applied on any XML document (will not be used), the wanted result is produced:

lang = en

Do note the use of the FXSL 1.x str-split-to-words template and the use of the EXSLT ext:node-set() extension function.

XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

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

     <xsl:param name="pQString" as="xs:string" select=
     "'?login=userId&amp;tag=XSLT&amp;lang=en&amp;level=expert'"
     />

    <xsl:template match="/">
      <xsl:variable name="vLang" as="xs:string" select=
      "substring-after(
                       tokenize($pQString, '\?|&amp;')
                                 [starts-with(.,'lang=')]
                                    [last()],

                       'lang='
                       )
      "/>

      lang = "<xsl:sequence select='$vLang'/>"
    </xsl:template>
</xsl:stylesheet>

When the above XSLT 2.0 transformation is performed, it produces the correct result:

  lang = "en"

这篇关于使用 xslt 的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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