simple xml - change tag name

Krzysztof Poc

Usenet Archive
Joined
Apr 30, 2009
Messages
15
Reaction score
0
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
 

Henry Law

Usenet Archive
Joined
Dec 30, 2003
Messages
349
Reaction score
0
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'} );
 

Willem (3)

Usenet Archive
Joined
Mar 16, 2011
Messages
234
Reaction score
0
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
 

Ben Morrow

Usenet Archive
Joined
Oct 26, 2003
Messages
4,961
Reaction score
0
Quoth Krzysztof Poc :
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>

...

Assuming your actual XML is larger and more nested than this, this sound to me like a job for a SAX filter.

Ben
 

Henry Law

Usenet Archive
Joined
Dec 30, 2003
Messages
349
Reaction score
0
On 05/12/11 14:54, Willem wrote:
Henry Law wrote:
)
) $somename->{surname} = $somename->{name};
) delete $somename->{name};

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

I missed that! Makes what the OP wants to do even easier.

Pleased to learn something more.
 

Willem (3)

Usenet Archive
Joined
Mar 16, 2011
Messages
234
Reaction score
0
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
 

Krzysztof Poc

Usenet Archive
Joined
Apr 30, 2009
Messages
15
Reaction score
0
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
 

Henry Law

Usenet Archive
Joined
Dec 30, 2003
Messages
349
Reaction score
0
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>

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.

By the way, the POD for XML::Simple says says that it makes certain assumptions, including this one:

You don't want fine-grained control of the formatting of generated XML

That should have told you what you wanted to know, if you'd read it.
 

Ben Morrow

Usenet Archive
Joined
Oct 26, 2003
Messages
4,961
Reaction score
0
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:

...

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
 

Krzysztof Poc

Usenet Archive
Joined
Apr 30, 2009
Messages
15
Reaction score
0
Quoth Henry Law :

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.
 

RedGrittyBrick

Usenet Archive
Joined
Feb 6, 2005
Messages
1,445
Reaction score
0
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.
 

Henry Law

Usenet Archive
Joined
Dec 30, 2003
Messages
349
Reaction score
0
On 09/12/11 12:10, RedGrittyBrick wrote:
Henry was wrong about that†, but his general point is valid.

Indeed I was wrong but this post explained the thinking that was in my mind, which isn't wrong: that textual equivalence is not _usually_ necessary for XML documents to be judged "the same" by the program that is processing them, and is therefore not often something that authors of XML tools strive for.
 

Willem (3)

Usenet Archive
Joined
Mar 16, 2011
Messages
234
Reaction score
0
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
 

Peter J. Holzer

Usenet Archive
Joined
Aug 15, 2000
Messages
1,908
Reaction score
0
[...]

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

Usenet Archive
Joined
Aug 15, 2000
Messages
1,908
Reaction score
0
†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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top