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

Is there a way to add a xml-stylesheet reference in xml twig? I've searched long and far yet the answer eludes me. Using twig v3.22

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

    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?

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

      Yep! I wasn't sure what a PI was. Thanks for your help.