Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.5K 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 11 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
Posted 11 years ago
POSTED BY: B B
Posted 11 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 11 years ago

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 11 years ago
POSTED BY: B B
Posted 11 years ago

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard