REXML Question

Discussion in 'Ruby' started by mike leonard, Sep 24, 2005.

  1. mike leonard

    mike leonard Guest

    I'm looking at REXML as a possible alternative to some things I've been
    doing with XSLT, but I haven't the foggiest notion of how to accomplish
    even the simplest transformations. Using REXML, how would I go about,
    say, translating all <em>'s into <emph>'s, or all <chapter>'s into <div
    type="chapter">'s?

    Thank you kindly in advance,

    Mike
    mike leonard, Sep 24, 2005
    #1
    1. Advertising

  2. mike leonard

    James Britt Guest

    mike leonard wrote:
    > I'm looking at REXML as a possible alternative to some things I've been
    > doing with XSLT, but I haven't the foggiest notion of how to accomplish
    > even the simplest transformations. Using REXML, how would I go about,
    > say, translating all <em>'s into <emph>'s, or all <chapter>'s into <div
    > type="chapter">'s?


    Have you tries brute-force regexp?

    If that isn't a suitable option, use the stream or pull parser to
    traverse the document and do the substitution as each element is
    encountered.

    There should be an article on this topic coming up in Dr. Dobbs, but it
    may not appear for a month or so.



    James


    --

    http://www.ruby-doc.org - The Ruby Documentation Site
    http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
    http://www.rubystuff.com - The Ruby Store for Ruby Stuff
    http://www.jamesbritt.com - Playing with Better Toys
    James Britt, Sep 24, 2005
    #2
    1. Advertising

  3. mike leonard

    Phlip Guest

    mike leonard wrote:

    > I'm looking at REXML as a possible alternative to some things I've been
    > doing with XSLT, but I haven't the foggiest notion of how to accomplish
    > even the simplest transformations. Using REXML, how would I go about,
    > say, translating all <em>'s into <emph>'s, or all <chapter>'s into <div
    > type="chapter">'s?
    >
    > Thank you kindly in advance,


    REXML is a DOM-style parser, not a transformer.

    To do what you said, off the top of my head I would ...

    doc = Document.new(File.read('some.xml'))
    XPath.each(doc, '//em') do
    |node|
    node.name = 'emph' # or similar; check the members first
    end
    doc.write(File.open(''another.xml')) # or similar to write

    There; a transformation in a bottle. Because XPath.each() returns the actual
    DOM nodes inside the parsed tree, and because writing on these nodes changes
    that tree, you can loop thru the nodes and manipulate them using clever
    XPath expressions. The system will accomodate most of the XML rules for you.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    Phlip, Sep 24, 2005
    #3
  4. mike leonard

    marcus Guest

    Fowler wrote about this a couple of years ago

    http://www.martinfowler.com/bliki/MovingAwayFromXslt.html

    /Marcus

    mike leonard wrote:
    > I'm looking at REXML as a possible alternative to some things I've been
    > doing with XSLT, but I haven't the foggiest notion of how to accomplish
    > even the simplest transformations. Using REXML, how would I go about,
    > say, translating all <em>'s into <emph>'s, or all <chapter>'s into <div
    > type="chapter">'s?
    >
    > Thank you kindly in advance,
    >
    > Mike
    >
    >
    >
    >
    marcus, Sep 24, 2005
    #4
  5. mike leonard

    mike leonard Guest

    Thanks for the replies. Actually, it was Fowler's article that piqued
    my interest in using REXML for transformations, but I didn't know the
    first thing about it. Phlip's example above is just what I was looking
    for. Incidentally, Phlip, what do you mean by "check the members first"?
    mike leonard, Sep 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damphyr
    Replies:
    2
    Views:
    137
    Damphyr
    Jul 16, 2003
  2. Ralph Mason
    Replies:
    2
    Views:
    85
    Ralph Mason
    Jan 13, 2004
  3. Han Holl

    Rexml xpath question

    Han Holl, Feb 22, 2004, in forum: Ruby
    Replies:
    7
    Views:
    138
    Han Holl
    Mar 6, 2004
  4. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Henrik Horneber
    Oct 12, 2004
  5. Phlip
    Replies:
    0
    Views:
    140
    Phlip
    Jan 15, 2008
Loading...

Share This Page