Page 1 of 1

Find in Files iwth XPath

Posted: Tue Feb 09, 2010 7:56 am
by csalsa
Hi

I have a number of XML files that I want to search to find examples of files that contain five or more elements named 'accountNumber' in the one file.

How can I use the 'Restrict to XPath' field in the 'Find/Replace in Files' dialog for this type of search. I have lots and lots of files with one or two elements 'accountNumber'.

I had thought to use an XPath expression such as:

Code: Select all

count(//accountNumber) > 5
but I could not get it to work. I have tried this and variations.

What would you suggest?

Re: Find in Files iwth XPath

Posted: Tue Feb 09, 2010 10:40 am
by adrian
Hello,

First I would like to ask you to update Oxygen to the latest build(2010020412) which includes a few fixes regarding Find/Replace in Files. You can find it here:
http://www.oxygenxml.com/download_oxygenxml_editor.html

The XPath is used for determining the intervals to be searched from a document, so the XPath result has to be a node-set.

What you want can be done with:

Code: Select all

/*[count(//accountNumber) > 5]
Please note that this will only search inside the root of the selected documents(that have more than 5 accountNumber elements).
And you will also have to use a Text to find to actually search for some text. accountNumber would be appropriate in your case.

Let me know if you need further help.

Regards,
Adrian

Re: Find in Files with XPath

Posted: Tue Aug 02, 2011 9:39 pm
by sarcanon
I'm having trouble with this function. I want to find instances of a particular element without regard to its contents. My XPath expression is: //div[@type='addendum']. I have verified that the Scope setting is correct.

If I put the XPath expression in the "Text to Find" field, I get back zero results, when I should get hundreds of hits. If I put the XPath expression in the "Restrict to XPath" field with an empty string in "Text to Find", Oxygen complains that I "Cannot search for a blank string", which makes sense, of course. If I put .* in the "Text to Find" and check the Regular Expression checkbox, I get back too many hits (one hit for each line within the <div>)> I only want one hit for each instance of the <div> element.

I am stumped. How do I find all instances of a particular element in all files in a project where the contents of the element is irrelevant?

I am running Oxygen 12.2 (build 2011062910) on Windows 7.

Thank you.

Re: Find in Files iwth XPath

Posted: Thu Aug 04, 2011 10:59 am
by adrian
Hello,

The XPath expression can only be used in the Restrict to XPath field, but you still have to search for something(in the Text to find field), the XPath only functions as a filter.

Note that the Find/Replace in Files works different than the Find/Replace in an editor. Find/Replace in Files searches one line at a time, so if the XPath result is on multiple lines, you will obtain multiple matches, one for each line.

If all you want is the location(start) or the number of occurrences of the element, you can search for <div (in the Text to find field) and use the mentioned XPath expression(in the Restrict to XPath field).

Let me know if you encounter further difficulties.

Regards,
Adrian

Re: Find in Files iwth XPath

Posted: Mon Jan 28, 2013 2:08 am
by sarcanon
More on this topic. How would I find files NOT containing a certain XPath expression?

Re: Find in Files iwth XPath

Posted: Mon Jan 28, 2013 11:36 am
by adrian
Hi,
sarcanon wrote:More on this topic. How would I find files NOT containing a certain XPath expression?
I'm afraid you can't, at least not by using negative find logic.
Find/Replace in Files is meant to search for content in files (hence the name), so you have to define a string or expression that you want to find inside the files (positive find).

You could use directly the XPath toolbar (or the XPath/XQuery Builder, Window > Show View >...) and write an XPath (2.0) that does this instead.
e.g.

Code: Select all

collection('file:/C:/path/to/folder/?select=*.xml')/*[not(//element)]
This obtains a list of all XML files (their root elements actually) that do not have "element" elements.

You can also make use of the current file directory (of the editor selected in Oxygen) instead of specifying the full folder URL.
e.g.

Code: Select all

collection('.?select=*.xml')/*[not(//element)]
Regards,
Adrian

Re: Find in Files iwth XPath

Posted: Mon Jan 28, 2013 6:26 pm
by sarcanon
That will work quite nicely.

Thank you.