I would like to extract from an XMLObject only the XMLElements with a specific tag.
Inspired from the Mathematica help (section transforming XML, introduction, the example with the colours), I have tried this code :
Definition of the XMLObject:
XMLtest =
XMLObject[
XMLElement["YO", {},
{XMLElement["YO", {}, 2],
XMLElement["BA", {},
{XMLElement["YO", {},
{XMLElement["BA", {}, 2]}], XMLElement["BA", {}, 2]}]}]]
Code so as to suppress all the elements different from "YO" in order to keep only the elements with the tag "YO":
XMLtest2 = XMLtest /. XMLElement[Except["YO"], _, _] -> Sequence[]
The above code works well.
Code so as to suppress the XMLElements without the tag "BA":
XMLtest2 = XMLtest /. XMLElement[Except"BA", _, _] -> Sequence[]
For this latter code, I have a problem with the answer because I obtain only the XMLObject. However, I would like to obtain a tree of XMLElements under the XMLObject.
Please help me to correct my code in this lastRoutine clean-up case?
\[RightArrow]
character when instead you need either plain text->
or\[Rule]
. Correct this and your replacement will work. I am closing this question as a "simple syntax error" now. If you find that I am mistaken or have other problem please comment here, starting your comment with@Mr.Wizard
so that I will be notified. $\endgroup$xmltest2 = Block[{XMLElement}, XMLElement["BA", __] := Sequence[]; XMLtest]
andxmltest3 = Block[{XMLElement}, XMLElement[Except["YO"], __] := Sequence[]; XMLtest]
. $\endgroup$DeleteCases
; do you find otherwise? $\endgroup$XMLElement
as a function. OTOH, there is a difference I overlooked at first. The OP's code would do one replacement, mine three, due toReplaceAll
vs. evaluation. So maybe my comment isn't so helpful here. $\endgroup$