Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I wrote this test code:
use strict; use warnings; use XML::Simple; my %h = ( 'name' => 'my name', 'values' => { 'A' => 'a', 'B' => 'b', 'C' => 'c', }, ); XMLout(\%h,outputfile=>'./test_ok.xml'); my %g = %{XMLin('./test_ok.xml')}; print "ok, so far\n"; %h = ( 'name' => 'my name', 'values' => { '01' => 'a', '001' => 'b', '0001' => 'c', }, ); XMLout(\%h,outputfile=>'./test_ko.xml'); %g = %{XMLin('./test_ko.xml')};
It gives me an error in the second case, and the only difference I could think of is that the keys are strings of formatted numbers...
Could someone explain where I'm wrong?
thank you very much in advance.

Replies are listed 'Best First'.
Re: writing hashes with XML::Simple
by jeffa (Bishop) on Jul 11, 2004 at 15:53 UTC

    I have not verified this by researching XML spec ... but i am fairly certain that you cannot use attributes that have leading digits. I know that my vi syntax hightlighter certainly differentiated between the two. ;)

    The error comes from trying to parse the the most recent file created ... XML::Simple simply does what you tell it to and creates the XML file with invalid attributes.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      i am fairly certain that you cannot use attributes that have leading digits.

      Too right you can't. Here's the dope: XML datatypes courtesy of www.w3.org. Specifically, what we are looking at are NCNames (non-colonized names).

      No, nothing to do with colonial imperialism, just the fact that the names don't have colons (:) in them, i.e. what happens whe XML namespaces are taken into account. And even then you can't have leading digits.

      The easiest way forward at this point would be to prefix the digital attributes with an underscore, that's legal (and Perl will treat _ as a no-op when lexing numbers, which in my books counts as a bonus).

      - another intruder with the mooring of the heat of the Perl