Page 1 of 1

How to delete items having nodes containing numbers

Posted: Mon Mar 13, 2017 1:05 pm
by martindue
Hi,

I love the software however I have a problem regarding a XML file.

The XML structure looks like this:

<PRODUCTS>
<PRODUCT>
<ID><![CDATA[1]]</ID>
<NAME>TEST</NAME>
</PRODUCT>
<PRODUCT>
<ID><![CDATA[ABC1]]</ID>
<NAME>Test with letters</NAME>
</PRODUCT>
</PRODOUCTS>

How can I search and delete all products with numbers only? Also I would like to know if it's possible to wrap nodes? Lets say I want to wrap all NAME nodes inside my PRODUCT with <INFO></INFO>

Re: How to delete items having nodes containing numbers

Posted: Mon Mar 13, 2017 3:47 pm
by adrian
Hi,
How can I search and delete all products with numbers only?
I'm guessing you mean all products with numeric IDs.
I'd recommend Find/Replace for this one. It's also possible with "XML Refactoring" (Elements > Delete Element, same XPath), but you'll lose the CDATA tags (not the data itself but the tags surrounding it).
Open Find > Find/Replace:
Find: (?s).*
Replace with: (leave empty)
XPath: //PRODUCT[matches(ID/text(), "^\d+$")]
[x] Regular expression
Press "Replace All".
Also I would like to know if it's possible to wrap nodes? Lets say I want to wrap all NAME nodes inside my PRODUCT with <INFO></INFO>
Normally you'd do this with XML Refactoring (Elements > Wrap element, XPath), but since that affects CDATA, we'll use Find/Replace again:
Open Find > Find/Replace:
Find: (?s).*
Replace with: <INFO>$0</INFO>
XPath: //NAME[ancestor::PRODUCT]
[x] Regular expression
Press "Replace All".

Note: XPath only works if your XML file is well-formed. Check for that with menu > Document > Validate > Check Well-Formedness.

Regards,
Adrian

Re: How to delete items having nodes containing numbers

Posted: Mon Mar 13, 2017 4:59 pm
by martindue
Thank you much, this is really appreciated.