in reply to Datastructures to XML

Wow, been looking how to approach that for awhile. I somehow knew it would take someone like Jenda to figure it out. Nested references in the hash values seem problemsome.

I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness

Replies are listed 'Best First'.
Re^2: Datastructures to XML
by Jenda (Abbot) on Mar 18, 2009 at 00:45 UTC

    I guess I can take this as a suggestion that it would be worth it to implement this, right? :-)

    You mean if the data look like this:

    [ { 'lname' => 'Krynicky', 'fname' => 'Jenda', 'PageId' => 1, 'Details' => { Header => 'blah blah', Footer => 'bla bla bla', } }, ... ];
    ? You could use a template like this for example:
    <root> <page cmd:foreach set:id="$_->{PageId}" opt:required> <field cmd:foreachkey="! /^(PageId|Details)$/"> <cmd:insert set:_tag="$key" set:_content="$value"/> </field> <details cmd:forkey="Details"> <cmd:insert cmd:foreachkey set:_tag="$key" set:_content="$value"/ +> </details> </page> </root>

    That is you make sure the foreachkey in the <field> tag skips both the PageId and Details keys, include the <details> tag to present the values of the 'Details' key and finally foreachkey directly in the <cmd:insert> tag to print the data from the inner hash.

    Is this what you meant? I assume writing the documentation for this will be quite hard. Just like the docs for XML::Rules were. And I don't think I did a perfect job there.

      Great, Ok, Mr. SmartyPants, now make it so those arrays can contain other hashrefs and arrayrefs, which themselves can contain any combo of references.

      I was just thinking.... is there a "recursive glob"? Like a glob to wildcard all subdirs (infinite depth)...but I think I will post a new node for something like that. :-)


      I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness

        The same way I showed in the previous example. You can nest the cmd:forXXX any way you like. You just have to know which keys/indexes point to arrayrefs, which to hashrefs and which to scalars. And use the cmd:forkey, cmd:forkeys or filter the cmd:foreach or cmd:foreachkey to select/filter them right. The filters specified as the values of the cmd:foreach and cmd:foreachkey attributes are Perl expressions that expect to find the key/index in $_, I'll have to provide access to the value or the whole array/hash as well so that you could filter using the values, butI think it should handle any combination of references. Provided that the data structure is consistent. That is, if it's an array of hashes of something then the value of the key {Foo} in all hashes has the same type.

        Plus actually there is no reason why couldn't the cmd:foreach handle plain scalars the same way as a ref to a one element array.