How to copy external CSS and JavaScript in XSLT

前端 未结 3 1715
死守一世寂寞
死守一世寂寞 2021-02-02 18:25

I have an XSL transformation which outputs HTML. In the head element I have a CSS file reference.



        
相关标签:
3条回答
  • 2021-02-02 18:48

    Use a processing instruction to wrap the CSS content:

    <?xml version="1.0" encoding="utf-8"?>
      <root>
        <?wrapper html
          <html>
            <link rel="stylesheet" type="text/css" href="css/styles.css"/>
          </html>
        ?>
      </root>
    

    Then tweak the existing xsl:copy-of select statement to render it:

     <xsl:copy-of select="document('css/styles.css')//processing-instruction()"/>
    
    0 讨论(0)
  • 2021-02-02 18:57

    Maybe you could trick it into thinking the stylesheet is XML.

    styles.css

    /*
    <?xml version="1.0" encoding="utf-8"?>
    <style>
    <![CDATA[
    */
    ... styles ...
    /*
    ]]>
    </style>
    */
    

    It's a hack but if there is no other way it might suffice (assuming it works at all).

    0 讨论(0)
  • 2021-02-02 19:05

    XSLT 2.0 provides the unparsed-text() function to read documents via URL that are not XML.

    In XSLT 1.0, if you don't need to be too script about the CSS, you can use the following to make the CSS file XML-compatible. And, fortunately, the browsers tolerate the HTML comments.

    CSS

    <!--/*--><root><![CDATA[<!--*/--> 
    body
    {
        margin: 0;
    }
    div > p
    {
        background-color: yellow;
    }
    <!--/*-->]]></root><!--*/--> 
    

    XSLT

    <style type="text/css">
        <xsl:value-of select="document('test.css')" disable-output-escaping="yes" />
    </style>
    
    0 讨论(0)
提交回复
热议问题