xQuery子串问题

发布于 2024-12-06 21:04:52 字数 493 浏览 0 评论 0原文

我现在有一个文件的完整路径作为字符串,例如:

   "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml" 

但是,现在我只需要取出文件夹路径,因此它将是上面的字符串,没有最后一个反斜杠内容,例如:

"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"

但似乎 substring() xQuery 中的函数只有 substring(string,start,len) 或 substring(string,start),我试图找出一种方法来指定反斜杠的最后一次出现,但没有运气。

专家可以帮忙吗?谢谢!

I now have a full path for a file as a string like:

   "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml" 

However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:

"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"

But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.

Could experts help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梅窗月明清似水 2024-12-13 21:04:52

尝试使用 tokenize() 函数(用于将字符串拆分为其组成部分),然后使用除最后一部分之外的所有内容重新组装它。

let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
    $segments := tokenize($full-path,"/")[position() ne last()]
return
  concat(string-join($segments,'/'),'/')

有关这些函数的更多详细信息,请查看它们的参考页面:

Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.

let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
    $segments := tokenize($full-path,"/")[position() ne last()]
return
  concat(string-join($segments,'/'),'/')

For more details on these functions, check out their reference pages:

初见 2024-12-13 21:04:52

fn:replace 可以使用正则表达式完成这项工作:

replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml", 
        "[^/]+$", 
        "")

fn:replace can do the job with a regular expression:

replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml", 
        "[^/]+$", 
        "")
许仙没带伞 2024-12-13 21:04:52

即使使用单个 XPath 2.0(XQuery 的子集)表达式也可以完成此操作

substring($fullPath, 
          1, 
          string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
          )

其中 $fullPath 应替换为实际字符串,例如 >:

"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"

This can be done even with a single XPath 2.0 (subset of XQuery) expression:

substring($fullPath, 
          1, 
          string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
          )

where $fullPath should be substituted with the actual string, such as:

"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
野心澎湃 2024-12-13 21:04:52

以下代码进行标记,删除最后一个标记,将其替换为空字符串,然后连接回来。

string-join(
  (
    tokenize(
        "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
        "/"
      )[position() ne last()],
    ""
  ),
  "/"
)

它似乎在 try.zorba-xquery.com 上返回了所需的结果。这有帮助吗?

The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.

string-join(
  (
    tokenize(
        "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
        "/"
      )[position() ne last()],
    ""
  ),
  "/"
)

It seems to return the desired result on try.zorba-xquery.com. Does this help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文