Message Boards Message Boards

0
|
7025 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Simplify an XMLObject limited to XMLElements with a specific tag

Posted 10 years ago

Hello,

I would like to simplify an XMLObject by only retrieving the XMLElements with a specific tag.

I would like a code based with the use of XMLElements as function.

I try this code :

XMLtest3 =  Cases[XMLtest, _XMLElement, Infinity] //. XMLElement[tag : "YO", attributes_, data_] :>  XMLElement[tag , attributes, data]

Definition of the XMLObject:

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

enter image description here

I would like to keep only the XMLElements with the tag "YO" and to preserve a tree structure for the XMLObject.

May you help me to correct the code given?

Thank you for your help

POSTED BY: B B
3 Replies
Posted 10 years ago

How about:

DeleteCases[XMLtest, XMLElement[tag_, attributes_, data_] /; tag != "YO", Infinity]
POSTED BY: Gustavo Delfino
Posted 10 years ago

Thank you for your answer. But, the Cases function permit to select all the XMLElements with the desired tag but the hierarchical structure (or tree) of the XMLElements is lost.

POSTED BY: B B

Since you want to preserve the tree structure, does this suit your needs?

Cases[XMLtest, XMLElement["YO", {}, _], Infinity]
POSTED BY: Jesse Friedman
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