Re: generating attributes and content for an element

Manuel Souto Pico <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Hi Michael,

Thank you very much for your reply. It did help. See below for my answers.

Michael Ludwig escribió:
> Manuel Souto Pico schrieb:
>   
>> I'm starting to learn XML with Perl (and I'm not an experienced Perl
>> programmer either), so I'd be thankful if you could be patient ;)
>>     
>
> Hi Manuel,
>
> as you're saying you're a newbie, and in case you don't already know,
> always start your programs with:
>
> use strict;
> use warnings;
>   
I didn't paste my whole script to save space, but I was already using 
those options. Thanks a lot for the hint anyway ;)
> That'll spare you a lot of trouble.
>
>   
>> The doubt that I have is about generating both content* and attributes
>> for an element.
>>
>> (*): content which is not other elements, that is.
>>
>> By means of
>>
>> my $data = {
>>        e => ['element value'],
>>        a => 'attribute value'*
>> *};
>>     
>
> This is a syntax error, and incomplete. You're using XML::Simple, which
> may become very complicated if what you want to do isn't simple enough.
>
>   
I'm sorry, what is the syntax error exactly? If you are referring to the 
absent comma, I though it wasn't needed after the last element before 
the block-closing bracket.
>> I can get  this:
>>
>> <root a="attribute value">
>>  <e>element value</e>
>> </root>
>>
>> However, what I'm after is something like
>>
>> <root>
>>  <e a="attribute value">element value</e>
>> </root>
>>     
>
> Take a look at the documentation and look for "ContentKey". XML::Simple
> uses nested data structures in Perl to represent XML, which falls short
> of representing XML. Hence one of several workarounds, to define a magic
> key "content" (or whatever you choose to name it using "ContentKey"),
>   
Thanks, I will check that.
> which allows you to add text content.
>
> See: perldoc XML::Simple
>
> milu@colinux:~ > cat /tmp/simple.pl
> use strict;
> use warnings;
> use XML::Simple;
> my $data = {
>      Salat => {
>          content => 'Gurken',
>          mit     => 'Tomaten',
>      }
> };
> print XMLout( $data, KeepRoot => 1);
>
> milu@colinux:~ > perl /tmp/simple.pl
> <Salat mit="Tomaten">Gurken</Salat>
>   
Could you believe that I had already tried this? :) Using the word 
'content' was my first guess, but it didn't work. I probably had the 
brackets wrong or something. But it does work now!
> I hope this helps.
>   
I did, thank you so much!! :)

By the way, in case you are interested, what I'm doing is converting a 
dictionary that I have in XML to Martif format. With XMLin I captured 
the original dictionary and now I'm trying to transfer the contents to 
the new structure.

Manuel
> Michael Ludwig
> _______________________________________________
> Perl-XML mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
>
>

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.