Search code examples
c#xpathcognos-bi

Parse xpath expression into file path


For whatever stupid reason, the API I'm querying returns file paths as XPath. I'm trying to figure out a way to parse it back into a file path. So for example,

/content/folder[@name='Dropbox']
----> content/Dropbox

/content/folder[@name='whatever']/folder[@name='something else']/report[@name='why']
----> content/whatever/something else/why

/content/folder[@name='A/B']
----> content/A/B

The best idea I have right now is to split the xpath by / delimiters and then comprehend the individual tags, but that will break on test case 3 above. Alternatively, I could just write a pushdown automata and do it myself. Is there an xpath reverse parser that I can use to process these strings into something intelligent?


Solution

  • Perhaps use a regular expression find and replace, look for substrings matching this regular expression:

    [^\/]*\[@name='([^']+)']
    

    ... and replace each match with the captured group. e.g. https://regex101.com/r/n42SqR/1