使用 XPath 将 URL 查询字符串解析为参数映射 [英] Parsing a URL query string into a map of parameters with XPath

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

问题描述

在 XSLT/XPath 3.0 中将 URL 查询字符串解析为 { 'param': 'value' } 映射的最易读的方法是什么?

What would be the most readable way to parse a URL query string into a { 'param': 'value' } map in XSLT/XPath 3.0?

注意:这是 使用 XPath 从参数映射构建 URL 查询字符串.

更新:我忽略了该函数应该支持多值参数,例如 a=1&a=2,并且理想情况下将它们解析为 xs:string*代码>序列.

Update: I neglected to mention that the function should support multi-value parameters such as a=1&a=2, and ideally parse them as an xs:string* sequence.

推荐答案

declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
let $querystring := "a=1&b=2&c=3"
return 
  ( tokenize($querystring, "&") 
    ! (let $param := tokenize(., "=") 
      return map:entry($param[1], $param[2]) ) 
  ) => map:merge()

为了支持多个值,您可以应用 $options 参数 指定如何处理duplicates:

In order to support multiple values, you could can apply the $options parameter specifying what to do with duplicates:

declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
let $querystring := "a=1&b=2&a=3"
return 
  ( tokenize($querystring, "&") 
    ! (let $param := tokenize(., "=") 
      return map:entry($param[1], $param[2]) ) 
  ) => map:merge(map:entry('duplicates', 'combine'))

这篇关于使用 XPath 将 URL 查询字符串解析为参数映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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