simple xml - change tag name

K

Krzysztof Poc

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
 
W

Willem

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
 
W

Willem

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
 
K

Krzysztof Poc

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'} );

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
 
K

Krzysztof Poc

Quoth Henry Law <[email protected]>:








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


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.
 
R

RedGrittyBrick

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.
 
W

Willem

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
 
P

Peter J. Holzer

[...]

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
 
P

Peter J. Holzer

†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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top