How to delete items having nodes containing numbers

Questions about XML that are not covered by the other forums should go here.
martindue
Posts: 2

How to delete items having nodes containing numbers

Mon Mar 13, 2017 1:05 pm

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>
adrian
Posts: 2326

Re: How to delete items having nodes containing numbers

Mon Mar 13, 2017 3:47 pm

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

Re: How to delete items having nodes containing numbers

Mon Mar 13, 2017 4:59 pm

Thank you much, this is really appreciated.

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests