Message Boards Message Boards

0
|
5122 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Suppress from a list of XMLElements those with a specific pattern

Posted 9 years ago

Hello,

I have a list of XMLElements. I would like to suppress those which have a name different from a specific name for example "YO". Here an example :

XMLtest = {XMLElement["YO", {}, 2],XMLElement["BA", {}, 2]}

Here my code

TRI = DeleteCases[XMLtest, XMLElement[_, _, _] /; XMLElement[_, _, _]<>XMLElement["YO", _, _], Infinity]

Consequently, i would like to obtain a list with only the XMLElement which has the name "YO".

I'm sorry if my question is quiet simple. I have started using mathematica not a long time ago.

Thanks a lot for your help.

POSTED BY: B B
5 Replies

Your minimal example can be solved with Except:

 DeleteCases[ XMLtest, XMLElement[Except@"YO", __] ]

but in general XMLElements are nested, so what if there is something inside YO? Do you want to suppress it or handle it more carefully? In my recent answer I've shown how to do the latter thing:

Retrieve XML Tree with only specific XMLElements

POSTED BY: Kuba Podkalicki
Posted 9 years ago

You totally right and thank you for your help. I want to remove all the XMLElements which have a label different from a specific one. As you mentioned, I should be cautious because the XMLElements are nested.

In fact, i would like to do the same as you have shown me in the post "Retrieve XML Tree with only specific XMLElements" but without creating it in notebook sheet. I would like only to transform my XML data.

I hope I was enough clear.

If yes, may you help me to give me some guidelines in order to create my new XMLObject without some XMLElements ?

Thank you for your help.

P.S : Thanks to this manipulation, I could build smaller example to show the different simplifications that I want to conduct.

POSTED BY: B B

Everything boils down to patterns and function construction (there are tutorials about it, take a look):

XMLtest = {
  XMLElement["YO", {}, 2], 
  XMLElement[  "BA", {},   {
        XMLElement["YO", {}, {
           XMLElement["BA", {}, 2]}],   XMLElement["BA", {}, 2]}]
  }

Block[{XMLElement},
 XMLElement["YO", _, {content__XMLElement}] := content;
 XMLElement["YO", _, _] := Sequence[];

 XMLtest
 ]
{XMLElement[ "BA", {}, {
        XMLElement["BA", {}, 2], XMLElement["BA", {}, 2]}]}
POSTED BY: Kuba Podkalicki
Posted 9 years ago

Thank you for your help and code proposal. The way you handle the nested XMLElements is perfect for me because it suppresses the unnecessary levels that I had before. As i wanted in this example only kept the XMLElements called "YO", I added the function "Except" and it works good.

XMLtest = Block[{XMLElement}, XMLElement[Except@"YO", , {content_XMLElement}] := content; XMLElement[Except@"YO", , ] := Sequence[]; XMLtest]

I need to keep now too types of XMLElements : XMLELements called "YO" and XMLElements called "TI". May you help me to modify this code in order I can add several conditions on the XMLElements that I want to keep in my XMLObjects. ?

Thank you for your help.

POSTED BY: B B
Posted 9 years ago

I simply my last question. Thank you in advance for your help

My question was : May you help me to modify this code in order I can add several conditions on the XMLElements that I want to keep in my XMLObjects. ? I need to keep now too types of XMLElements : XMLELements called "YO" and XMLElements called "TI".

POSTED BY: B B
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract