使用 XPath 拆分节点值

发布于 2024-10-03 14:23:46 字数 331 浏览 3 评论 0原文

XPath 中有某种 split() 函数吗? 假设我有这个 XML:

<root>
   <path>C:\folder\filename</path>
</root>

并且我想检索文件名,我该怎么做?我知道我可以像这样获取节点值:

//path/text()

How can I get only the filename? (我知道有一个 concat() 函数,所以也许有一个 split() 函数?)

Is there some kind of split() function in XPath?
Say I have this XML:

<root>
   <path>C:\folder\filename</path>
</root>

And I want to retrieve filename, how can I do this? I know I can get the node value like this:

//path/text()

How can I get only the filename? (I know there is a concat() function, so maybe there is a split() function?)

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

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

发布评论

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

评论(2

天暗了我发光 2024-10-10 14:23:46

如果您有支持 xpath-2.0 的 API,则可以通过两种方式解决此问题:

替换技术

尝试使用:

fn:replace(string,pattern,replace)

例如

fn:replace(//path/text(),".*/","")

tokenize 技术

您可能会从 tokenize 中获得一些收益:

fn:tokenize(string,pattern)

例如 (感谢 Martin)

tokenize(/root/path, '\\')[last()]

w3schools xml 处理“xsl 函数”文档

If you have an xpath-2.0 capable API, you can solve this in two ways:

replace technique

Try using:

fn:replace(string,pattern,replace)

e.g.

fn:replace(//path/text(),".*/","")

tokenize technique

You may get some mileage from tokenize:

fn:tokenize(string,pattern)

e.g. (thanks to Martin)

tokenize(/root/path, '\\')[last()]

w3schools xml processing "xsl functions" documentation

养猫人 2024-10-10 14:23:46

虽然我会使用

tokenize(/*/*, '\\')[last()]

还有许多其他方法来获取所需的字符串

  codepoints-to-string
    (reverse
      (string-to-codepoints
         (substring-before
            (codepoints-to-string
                 (reverse
                    (string-to-codepoints(/*/*)
                  )
              ),
              '\'
            )
          )
       )
     )

或者

  substring(/*/*,
            index-of(string-to-codepoints(/*/*),
            string-to-codepoints('\')
            )
            [last()]
          + 1
           )

While I would use:

tokenize(/*/*, '\\')[last()]

there are also numerous other ways to obtain the desired string:

  codepoints-to-string
    (reverse
      (string-to-codepoints
         (substring-before
            (codepoints-to-string
                 (reverse
                    (string-to-codepoints(/*/*)
                  )
              ),
              '\'
            )
          )
       )
     )

Or:

  substring(/*/*,
            index-of(string-to-codepoints(/*/*),
            string-to-codepoints('\')
            )
            [last()]
          + 1
           )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文