simple xml - change tag name

Discussion in 'Perl Misc' started by Krzysztof Poc, Dec 5, 2011.

  1. Hi

    I would like to change the tag name in an XML document. Can I do that
    using
    XML::Simple. If so could you give me a simple example of how to do
    that.

    To be exact I would like to convert the following:

    <album>
    <name> John </name>
    <age> 25 </age>
    </album>

    into:

    <album>
    <surname> John </surname>
    <age> 25 </age>
    </album>

    thank you for help
    Krzysztof Poc, Dec 5, 2011
    #1
    1. Advertising

  2. Krzysztof Poc

    Willem Guest

    Henry Law wrote:
    ) What you want to do isn't hard, given that structure. Something like
    ) this (untested) would work:
    )
    ) $somename->{surname} = $somename->{name};
    ) delete $somename->{name};

    Did you know that delete returns the element(s) it deleted:
    $somename->{surname} = delete $somename->{name};


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Dec 5, 2011
    #2
    1. Advertising

  3. Krzysztof Poc

    Willem Guest

    Krzysztof Poc wrote:
    ) I would like to change the tag name in an XML document. Can I do that
    ) using XML::Simple. If so could you give me a simple example of how to
    ) do that.

    Why would you want to do that with Perl?
    This is one of the few things that XSLT is actually good at!


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Dec 5, 2011
    #3
  4. On Dec 5, 2:51 pm, Henry Law <> wrote:
    > On 05/12/11 12:54, Krzysztof Poc wrote:
    >
    > > Hi

    >
    > > I would like to change the tag name in an XML document. Can I do that
    > > using
    > > XML::Simple. If so could you give me a simple example of how to do
    > > that.

    >
    > XML::Simple won't do it directly (the word "rename" doesn't appear in
    > its CPAN page!); its main function is to parse some XML and present its
    > content as a Perl hash (or the other way round).  As you would have
    > found if you'd tried it, your XML example will, with default settings,
    > turn up like this:
    > $somename = {
    >            'name' => ' John ',
    >            'age' => ' 25 '
    >          };
    >
    > (where $somename is the variable you used for the XMLin function).
    >
    > What you want to do isn't hard, given that structure.  Something like
    > this (untested) would work:
    >
    >    $somename->{surname} = $somename->{name};
    >    delete $somename->{name};
    >
    > If you want to learn some other XML module to do this -- because there's
    > more to your problem than you've explained so far, perhaps -- then fire
    > away and someone here will help you debug your code.  Me, I'd use
    > XML::Simple since I'm very familiar with it and the code above isn't
    > hard.  If I had to do it a lot I might even write a function which got
    > invoked like this
    >
    > rename_hash_key( $somename, { name=>'surname', otherkey=>'itsnewname'} );
    >
    > --
    >
    > Henry Law            Manchester, England


    Thanks for help. It works. Unfortunately the output XML does not
    resemble the
    input XML. The format differs a lot. I used "AttrIndent => 1" and it
    still differs a
    lot. Can I force Simple::XML to produce exactly the same text format
    for output
    as for input.

    thanks for help
    Krzysztof Poc, Dec 6, 2011
    #4
  5. On Dec 6, 3:42 pm, Ben Morrow <> wrote:
    > Quoth Henry Law <>:
    >
    >
    >
    > > On 06/12/11 11:24, Krzysztof Poc wrote:

    >
    > > > Thanks for help. It works. Unfortunately the output XML does not
    > > > resemble the
    > > > input XML. The format differs a lot. I used "AttrIndent =>  1" and it
    > > > still differs a
    > > > lot. Can I force Simple::XML to produce exactly the same text format
    > > > for output

    >
    > > One significant point about XML is that its textual format doesn't
    > > matter, at least not in every respect.  For example:

    >
    > > <sometag foo="bar" bar="blatz">
    > >    <anothertag/>
    > > </sometag>

    >
    > > is in XML terms exactly equivalent to

    >
    > > <sometag bar="blatz" foo="bar"><anothertag></anothertag></sometag>

    >
    > No it isn't. Whitespace within an element is part of the character data
    > for that element (after newline normalisation).
    >
    > > So your exact-equivalence of the text format (a) doesn't really make
    > > sense, as far as you've explained it; and (b) isn't really the domain of
    > > an XML parser.

    >
    > Preserving <foo/> vs. <foo></foo> is not the job of a XML parser.
    > Passing all the character data, including whitespace, is.
    >
    > In practice, many applications of XML don't care about whitespace, and
    > XML::Simple was written for them. But a decent general-purpose XML
    > parser ought to preserve it.
    >
    > Ben


    Thanks for help. I started using XML:TreePP module and it makes most
    of the
    things to me.
    Krzysztof Poc, Dec 7, 2011
    #5
  6. On 06/12/2011 14:42, Ben Morrow wrote:
    >
    > Quoth Henry Law<>:
    >> On 06/12/11 11:24, Krzysztof Poc wrote:
    >>
    >> One significant point about XML is that its textual format doesn't
    >> matter, at least not in every respect. For example:
    >>
    >> <sometag foo="bar" bar="blatz">
    >> <anothertag/>
    >> </sometag>
    >>
    >> is in XML terms exactly equivalent to
    >>
    >> <sometag bar="blatz" foo="bar"><anothertag></anothertag></sometag>

    >
    > No it isn't. Whitespace within an element is part of the character data
    > for that element (after newline normalisation).
    >


    Henry was wrong about that†, but his general point is valid.

    <dog:foo xmlns:dog='http://example.com/1'
    xmlns:cat='http://example.com/2'>
    <dog:bar>
    <cat:mint>12 9</cat:mint>
    </dog:bar>
    <dog:baz></dog:baz>
    </dog:foo>

    Is canonically equivalent to

    <foo xmlns="http://example.com/1">
    <bar>
    <horse:mint xmlns:horse="http://example.com/2">12 9</horse:mint>
    </bar>
    <baz />
    </foo>

    E&OE

    So people writing XML, probably need to understand the rather
    complicated rules in XML for deciding whether two XML documents are the
    same.


    †I don't really understand what is the benefit of preserving white-space
    in elements that are *not* leaf elements. But that's the way XML is.

    --
    RGB
    RedGrittyBrick, Dec 9, 2011
    #6
  7. Krzysztof Poc

    Willem Guest

    RedGrittyBrick wrote:
    ) So people writing XML, probably need to understand the rather
    ) complicated rules in XML for deciding whether two XML documents are the
    ) same.
    )
    )
    ) ???I don't really understand what is the benefit of preserving white-space
    ) in elements that are *not* leaf elements. But that's the way XML is.

    That's because XML is a markup language, not a data language.

    It just got overhyped and is now being used for all sorts of things it was
    never designed for. Which, in itself, is the basis for plenty of rants.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Dec 9, 2011
    #7
  8. On 2011-12-06 14:42, Ben Morrow <> wrote:
    > Quoth Henry Law <>:
    >> On 06/12/11 11:24, Krzysztof Poc wrote:
    >>
    >> > Thanks for help. It works. Unfortunately the output XML does not
    >> > resemble the
    >> > input XML. The format differs a lot. I used "AttrIndent => 1" and it
    >> > still differs a
    >> > lot. Can I force Simple::XML to produce exactly the same text format
    >> > for output


    [...]

    > In practice, many applications of XML don't care about whitespace, and
    > XML::Simple was written for them.


    Even worse, XML::Simple doesn't preserve order. For example, parsing

    <html>
    <body>
    <h1> Headline </h1>
    <h2> Section 1 </h2>
    <p> A paragraph. </p>
    <p> Another one. </p>
    <h2> Section 2 </h2>
    <p> Paragraph in section 2. </p>
    </body>
    </html>

    produces:

    {
    'body' => {
    'h1' => ' Headline ',
    'p' => [
    ' A paragraph. ',
    ' Another one. ',
    ' Paragraph in section 2. '
    ],
    'h2' => [
    ' Section 1 ',
    ' Section 2 '
    ]
    }
    };


    hp


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
    Peter J. Holzer, Dec 13, 2011
    #8
  9. On 2011-12-09 12:10, RedGrittyBrick <> wrote:
    > †I don't really understand what is the benefit of preserving white-space
    > in elements that are *not* leaf elements. But that's the way XML is.
    >


    <span><big>A</big>void</span> is not the same as
    <span><big>A</big> void</span> (and shouldn't be).

    hp


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
    Peter J. Holzer, Dec 13, 2011
    #9
    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. shruds
    Replies:
    1
    Views:
    791
    John C. Bollinger
    Jan 27, 2006
  2. Replies:
    4
    Views:
    439
  3. David  Portabella
    Replies:
    2
    Views:
    426
    Mike Schilling
    Jul 28, 2007
  4. David  Portabella
    Replies:
    3
    Views:
    3,534
    David Portabella
    Aug 14, 2007
  5. Erik Wasser
    Replies:
    5
    Views:
    452
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page