Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split node value with XPath

Tags:

xml

xpath-2.0

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?)

like image 214
Rise_against Avatar asked Nov 23 '10 10:11

Rise_against


2 Answers

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

like image 194
Alex Brown Avatar answered Oct 31 '22 18:10

Alex Brown


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
           )
like image 34
Dimitre Novatchev Avatar answered Oct 31 '22 18:10

Dimitre Novatchev