in reply to xml::twig and stylesheets

What is an "xml-stylesheet reference" exactly?

If you mean a processing that attaches an xsl stylesheet (eg <?xml-stylesheet type= "text/xls" href="xls_style.xlss"?>, then it's a processing instruction, and can be inserted as a regular PI: create an element with a tag of '#PI' and set its target to xml-stylesheet (using set_target) and its data to type= "text/xls" href="xls_style.xlss" (using set_data).

Does this help?

Replies are listed 'Best First'.
Re^2: xml::twig and stylesheets
by rogue90 (Novice) on Mar 22, 2006 at 21:32 UTC
    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.

      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 ;-)

        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.
Re^2: xml::twig and stylesheets
by rogue90 (Novice) on Mar 22, 2006 at 20:26 UTC
    Yep! I wasn't sure what a PI was. Thanks for your help.