Oxygen XML Multiple File Question

Questions about XML that are not covered by the other forums should go here.
bluerift
Posts: 4
Joined: Tue Jun 05, 2012 10:35 pm

Oxygen XML Multiple File Question

Post by bluerift »

I'm currently trying to edit 8,000 XML files that are all in 8,000 separate folders.

In between nodes 2 and 3, I need to insert a new node. This node would be the same throughout the 8000 files that are in the 8000 separate folders. My problem is I do not want to do this manually because it is going A LONG TIME.

Can someone help me figure out if Oxygen can do this within it's GUI?

If someone can help me, I would HIGHLY appreciate it!

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<1>
<2>
<New Node>
<3>
<4>
<5>
</Properties>
</DocClass>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Oxygen XML Multiple File Question

Post by adrian »

Hi,

If you simply want to insert a sibling between "2" and "3" you could use the Find -> Find/Replace in Files tool to do this.
First create a backup copy of your files.
Your example is a bit broken XML-wise so I'm using a well-formed one. So, for:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<a1/>
<a2/>
<a3/>
<a4/>
<a5/>
</DocClass>
to insert a newNode between a2 and a3 you can:
Search for:
<a3
Restrict to XPath (this restricts the search to all a3 elements preceded by an a2 element):
//a3[preceding-sibling::*[1][name() = 'a2']]
Replace with ($0 puts back the searched match: <a3):
<newNode/>$0

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
bluerift
Posts: 4
Joined: Tue Jun 05, 2012 10:35 pm

Re: Oxygen XML Multiple File Question

Post by bluerift »

Adrian,

Unfortunately after I followed your example, a new node was not placed in between nodes a2 and a3. Only a3 was replaced.

Using this XML:

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<a1/>
<a2/>
<a3/>
<a4/>
<a5/>
</DocClass>

I went to Find/Replace and entered these variables into the following text boxes.

Text To Find: <a3

Replace With: <newNode/>$0

XPath: //a3[preceding-sibling::*[1][name() = 'a2']]

After clicking replace, my code is now incorrect.

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<a1/>
<a2/>
<newNode/>$0/>
<a4/>
<a5/>
</DocClass>

If you could please help me figure out what I'm doing wrong, I would highly appreciate it!
Costin
Posts: 833
Joined: Mon Dec 05, 2011 6:04 pm

Re: Oxygen XML Multiple File Question

Post by Costin »

Hello,

In addition to what my colleague Adrian advised, could you please also enable the "Regular expresion" checkbox in the Find/Replace dialog and see if that gives you the desired results ?

Best Regards,
Costin
Costin Sandoi
oXygen XML Editor and Author Support
bluerift
Posts: 4
Joined: Tue Jun 05, 2012 10:35 pm

Re: Oxygen XML Multiple File Question

Post by bluerift »

Costin,

After checking the Regular Expression in the Find/Replace dialog underneath options. This is the code that is being shown.

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<a1/>
<a2/>
<newNode/><a3/>
<a4/>
<a5/>
</DocClass>


I want it to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<DocClass>
<a1>
<a2>
<newNode>
<a3>
<a4>
<a5>
</DocClass>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Oxygen XML Multiple File Question

Post by adrian »

If you want the new node on a separate line, insert a new line in the replace string. Use Replace With:
<newNode/>\n$0

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
bluerift
Posts: 4
Joined: Tue Jun 05, 2012 10:35 pm

Re: Oxygen XML Multiple File Question

Post by bluerift »

Adrian,

Use Replace With: <newNode/>\n$0

Worked absolutely perfectly for me.
___________________________________________________

I have another question for you kind sir.

I now need to put a new node on a separate line between a child node and a parent node. The newNode needs to be located between the parent <Properties> and <a1/> the child.

<DocClass>
<Properties>
<newNode>
<a1/>
<a2/>
<a3/>
</Properties>

In order to successfully accomplish this, what information would I need to fill into the the following text boxes?

Text To Find: ?

Replace With: ?

XPATH: ?
Costin
Posts: 833
Joined: Mon Dec 05, 2011 6:04 pm

Re: Oxygen XML Multiple File Question

Post by Costin »

Hi,

You can achieve what you want in 2 ways.
1. The first and the easier one would be to place <a1 in the Text to find filed, and replace it with the same as previously <newNode/>\n$0 value. As the XPath condition, use

Code: Select all

//a1[parent::Properties/*[1][self::a1]]
. This will find all the "a1" childs of the "Properties" element, starting with the first one.

2. However, a more complex but more accurate way to restrict the match only to the first occurrence of the <a1> element and also only if it is the first child of "Properties" would be to use the

Code: Select all

//*[1][self::a1 and parent::Properties]
XPath expression.

You should choose any of the solutions proposed above, depending on your XML structure and on what you want to achieve.

Regards,
Costin
Costin Sandoi
oXygen XML Editor and Author Support
Post Reply