xmlxpath

A tool for returning an absolute static xpath pointing to a text node containing some string in xml file


I need to find some record by its contents in a huge xml database. I am looking for a tool able to return a full static xpath pointing to a text node containing 'search string' in xml file.

Assume my xml file looks like:

<root>
  <a id="first">
    <b>Some text</b>
  </a>
  <a id="second">
    <b>The search string inside a longer text</b>
  </a>
</root>

If I use next xpath:

//text()[contains(., 'search string')]

it will get

The search string inside a longer text

which is correct, but not what I need. I wonder if there is a simple way to get as output or result:

/root/a[2]/b/

or

/root/a[@id='second']/b/

without coding in xslt or any xml oriented language.


Solution

  • I am looking for a tool able to return a full static xpath pointing to a text node containing 'search string' in xml file.

    and the path()-function might be what you're looking for.

    :

    xidel -s input.xml -e ^"^
      //text()[contains(.,'search string')]/replace(path(),'Q\{\}','')^
    "
    /root[1]/a[2]/b[1]/text()[1]
    

    :

    xidel -s input.xml -e '
      //text()[contains(.,"search string")]/replace(path(),"Q\{\}","")
    '
    /root[1]/a[2]/b[1]/text()[1]
    

    See also this xidelcgi demo.