Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

XML parsing using patterns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81537] XML parsing using patterns
  • From: dflatin at rcn.com
  • Date: Wed, 26 Sep 2007 06:56:24 -0400 (EDT)

I have an XML data structure where I want to extract the start and end
times, for example:

timeBlock = XMLElement["time-layout", {"time-coordinate" -> "local",
  "summarization" -> "none"}, {XMLElement[
   "layout-key", {}, {"k-p12h-n14-1"}],
  XMLElement["start-valid-time", {}, {"2007-09-21T20:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-22T08:00:00-04:00"}],
  XMLElement["start-valid-time", {}, {"2007-09-22T08:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-22T20:00:00-04:00"}],
  XMLElement["start-valid-time", {}, {"2007-09-22T20:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-23T08:00:00-04:00"}],
  XMLElement["start-valid-time", {}, {"2007-09-23T08:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-23T20:00:00-04:00"}],
  XMLElement["start-valid-time", {}, {"2007-09-23T20:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-24T08:00:00-04:00"}],
  XMLElement["start-valid-time", {}, {"2007-09-24T08:00:00-04:00"}],
  XMLElement["end-valid-time", {}, {"2007-09-24T20:00:00-04:00"}]}]

I can do this by finding all start times and all end times and then
combining them, as in

getTimeSequence[timeBlock_] :=
 Module[{startBlock, endBlock, startT, endT},
  startBlock = Cases[timeBlock,
    XMLElement["start-valid-time", _, {startT_}] :> startT,
    Infinity];
  endBlock = Cases[timeBlock, XMLElement["end-valid-time", _,
{endT_}] :> endT,
    Infinity];
  Transpose[{startBlock, endBlock}]
  ]

Philosophically, I think I should be able to capture the start and end
times with a single pattern, but I can't make it work. For example I
have tried:

getTimeStartStopPairs[timeBlock_] :=
 Module[{startBlock, endBlock, startT, endT},
  Cases[
   timeBlock,
   PatternSequence[XMLElement["start-valid-time", _, {startT_}],
     XMLElement["end-valid-time", _, {endT_}]] :> {startT, endT},
   Infinity
   ]
  ]

Does anyone have any suggestions? I feel like I am misinterpreting how
PatternSequence works.

Thanks,
Dan



  • Prev by Date: Re: adding rows to a table of data by looking in the table
  • Next by Date: Re: making a list using table but with different increment after a certain number
  • Previous by thread: Re: Good Introductory Text with a Physics Slant
  • Next by thread: PRINCIPAL VALUE + 3D NINTEGRATE