MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

RuleDelayed for parsing XML with multiple children

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109591] RuleDelayed for parsing XML with multiple children
  • From: Zach Bjornson <bjornson at mit.edu>
  • Date: Fri, 7 May 2010 06:29:00 -0400 (EDT)

Hi,

I'm trying to extract multiple values (same depth, different physical 
level) from an XML tree using RuleDelayed, but I realized that using 
RuleDelayed only extracts one of the children. That is:

XmlTree follows the form:
<LevelA>
<LevelB>
<Child>value1</Child>
</LevelB>
<LevelB>
<Child>value2</Child>
</LevelB>
</LevelA>

I want those two values.

Cases[XmlTree, 
XMLElement["LevelA",_,{___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValue_}],___}],___}]:>{WantThisValue},Infinity]

This only gives value1. I can explicitly add more XMLElement tags to get 
the other value

Cases[XmlTree, 
XMLElement["LevelA",_,{___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValue_}],___}],___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValueToo_}],___}]:>{WantThisValue,WantThisValueToo},Infinity]

but I don't want to use an explicit structure because the number of 
children/values varies.

Any have suggestions for the best alternative method to the RuleDelayed 
syntax I'm using? Changing the innermost XMLElement tag to simply 
XMLElement["Child",_,_] gives the proper answer, albeit with the messy 
flanking tree structure. Dropping indices is not an ideal solution.

Thanks!
Zach


  • Prev by Date: HoldFirst, Unevaluated
  • Next by Date: Plot3D problems: no plot in some cases
  • Previous by thread: Re: HoldFirst, Unevaluated
  • Next by thread: Re: RuleDelayed for parsing XML with multiple children