Find in Files iwth XPath

<oXygen/> general issues.
csalsa
Posts: 97
Joined: Tue Apr 22, 2008 9:31 am

Find in Files iwth XPath

Post by csalsa » Tue Feb 09, 2010 7:56 am

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?

adrian
Posts: 2679
Joined: Tue May 17, 2005 4:01 pm

Re: Find in Files iwth XPath

Post by adrian » Tue Feb 09, 2010 10:40 am

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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

sarcanon
Posts: 32
Joined: Mon Aug 24, 2009 11:42 pm

Re: Find in Files with XPath

Post by sarcanon » Tue Aug 02, 2011 9:39 pm

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.

adrian
Posts: 2679
Joined: Tue May 17, 2005 4:01 pm

Re: Find in Files iwth XPath

Post by adrian » Thu Aug 04, 2011 10:59 am

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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

sarcanon
Posts: 32
Joined: Mon Aug 24, 2009 11:42 pm

Re: Find in Files iwth XPath

Post by sarcanon » Mon Jan 28, 2013 2:08 am

More on this topic. How would I find files NOT containing a certain XPath expression?

adrian
Posts: 2679
Joined: Tue May 17, 2005 4:01 pm

Re: Find in Files iwth XPath

Post by adrian » Mon Jan 28, 2013 11:36 am

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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

sarcanon
Posts: 32
Joined: Mon Aug 24, 2009 11:42 pm

Re: Find in Files iwth XPath

Post by sarcanon » Mon Jan 28, 2013 6:26 pm

That will work quite nicely.

Thank you.

Post Reply