http://qs1969.pair.com?node_id=538609


in reply to Re: xml::twig and stylesheets
in thread xml::twig and stylesheets

how do I get the PI to appear at the beginning of the document?
my $xsl = new XML::Twig::Elt('#PI'); $xsl->set_target('xml-stylesheet'); $xsl->set_data('type= "text/xls" href="xls_style.xls"'); $xsl->paste('before', $twig->root);
This fails because you can't paste before the root.

Replies are listed 'Best First'.
Re^3: xml::twig and stylesheets
by mirod (Canon) on Mar 23, 2006 at 05:49 UTC

    Now you are entering the dark realm of comments and processing instructions before and after the root... be afraid, very afraid!

    The answer: you have to replace the paste by $twig->_add_cpi_outside_of_root( leading_cpi => $xsl);. Which I agree is slightly weird, not to mention totally undocumented.

    This might actually be worth a method of its own. What about add_stylesheet( $stylesheet_url )?

      Stylesheets aren't the only thing I've seen outside of root. To be honest, the way that rogue90 showed is the most intuitive manner to me. If XML::Twig could detect that the request is before the root AND the request is for a comment or a processing instruction, then just call the _add_cpi_outside_of_root function. Or if the request is after the root AND the request is for a PI, then do the same. (Comments after root allowed? I'm not sure.)

      Having an add_stylesheet method would then merely be a convenience method to wrap all that up. But it wouldn't limit the user to just one type of PI outside of root.

      Note that the way I've done this type of thing in the past is to read the XML file, add <root>...</root> tags around it, do my massaging, and then return $twig->root()->sprint($t->root(), 1) to get rid of the actual root tags. That can be ugly, too ;-)

        If XML::Twig could detect that the request is before the root AND the request is for a comment or a processing instruction, then just call the _add_cpi_outside_of_root function. Or if the request is after the root AND the request is for a PI, then do the same. (Comments after root allowed? I'm not sure.)

        It makes sense, I'll do just that (yes comments are allowed after the root, it's in the first production rule in the spec, the Misc after the element).

      hmm.. I don't have that method in the version I am using which I can't change. I found _add_prolog_data which I am attempting to use now but no luck as of yet. A method for adding stylesheets would be quite useful. My other option it seems is to create the prolog myself by removing it from the twig altogether. Thanks much for your help.