DOM / XPath combination

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phoebian

    #1

    DOM / XPath combination

    Hi all,

    Here is a little concept problem, and I need some guideline. I am
    currently developing a application which delivers XML files. I want
    then to sequentially perform some actions on selected part of that XML
    file through a XPath expression. These action have actually to modify
    the structure of the XML file itself and that seems to me like a big
    hurdle. Maybe I am wrong but I will try to explain what I want to
    do...

    the XML file ressembles that... very roughly :)

    <Root>
    <Obj1>
    <Head action="yes">bl ah</Head>
    <Body>blah</Body>
    <Foot action="yes">bl ah</Foot>
    </Obj1>
    ...
    <Obj2>
    <Head>blah</Head>
    <Body action="yes">bl ah</Body>
    <Foot>blah</Foot>
    </Obj2>
    </Root>

    let say my XPath expression is '//*[@action="yes"]'. Now for each of
    the 3 concerned element I want to do some formatting...

    <Root>
    <Obj1>
    <Head action="yes">
    <bonus>yo</bonus> <--- added as part of the formatting
    <text>blah<text > <--- formatting
    </Head>
    <Body>blah</Body>
    <Foot action="yes">
    <text>blah<text > <--- formatting
    <bonus>yo</bonus> <--- added as part of the formatting
    </Foot>
    </Obj1>
    ...
    <Obj2>
    <Head>blah</Head>
    <Body action="yes">
    <text>blah<text > <--- formatting
    </Body>
    <Foot>blah</Foot>
    </Obj2>
    </Root>

    Here is my problem. I first convert my XML file to a DOM
    representation, in order to be able to do sequential processing.
    Indeed a mere XSLT transfo is of no help since I want to be able to do
    many thing between two actions. I then do the XPath evaluation but
    further modification of the resulting nodelist will never be reflected
    in the original XML files. I should say that I am using python 2.3,
    and Domlette/XPath from 4suite to do that.

    My question is:

    How can I use the resulting XPath result as a live representation of
    the underlying XML nodes (DOM concept) so that I can both perform
    sequential action (by traversing the resulting xpath nodelist) and
    reflects modifications done on those same nodes in the original XML
    file?

    Maybe I am wrong on the live object concept of the DOM but I thought
    it would be possible. Any idea to achieve that would be welcome...

    Phoebian
  • Uche Ogbuji

    #2
    Re: DOM / XPath combination

    phoebian@free.f r (Phoebian) wrote in message news:<57279ad0. 0405130858.24fd 08e2@posting.go ogle.com>...
    [color=blue]
    > How can I use the resulting XPath result as a live representation of
    > the underlying XML nodes (DOM concept) so that I can both perform
    > sequential action (by traversing the resulting xpath nodelist) and
    > reflects modifications done on those same nodes in the original XML
    > file?
    >
    > Maybe I am wrong on the live object concept of the DOM but I thought
    > it would be possible. Any idea to achieve that would be welcome...[/color]

    Actually, the results of 4XPath node set expressions are lists of
    actual Domlette nodes, and you can use the mutation API on these
    nodes. For the API see:



    You can then use Domlette.Print( node) to reflect the updates back to
    XML.

    Did you try any particular mutations with no effect (or ill effect)?

    FWIW, you might want to consider XUpdate, which is also implemented in
    4Suite. I happen to cover it in my most recent ORA article:

    www.XML.com,Textuality Services,Uche Ogbuji,Programming,XML Namespaces Support in Python Tools, Part Two


    --Uche
    Igbo-American immigrant from Nigeria, settled near Boulder, Colorado with my wife, three sons and daughter. Restless mind in a restless body, I do a million things without getting very much truly done

    Comment

    Working...