XHTML: Move CSS into every tag from the <style> header to a @style attribute like a "premailer" with XSLT, possible?
On the Internet are several programs in e.g. Ruby or Python which move XHTML CSS styles into tags. They are often called premailer because some older mail programs have problems with header defined stylesheets. Here is a example what a python premailer does: premailer on pypi
What I want to do is to move the json formatted CSS stylesheets from the HTML <style>
header inside every related node which has a class
attribute.
Example input:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sometest</title>
<style type="text/css">
/*<![CDATA[*/
ol{margin:0;padding:0}p{margin:0}.c1{vertical-align:top;width:93.6pt;border-style:solid}.c2{vertical-align:top;width:41.8pt;border-style:solid}hr.c1{page-break-before:always}
/*]]>*/
</style>
</head>
<body>
<div>
<p class="c1"><span>Style C2 Text</span></p>
<hr class="c1"/>
<p class="c2">Style C1 Text</p>
</div>
</body>
</html>
My desired output. Look at style
in开发者_如何学Python <hr>
and <p>
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sometest</title>
<style type="text/css">
/*<![CDATA[*/
ol{margin:0;padding:0}p{margin:0}.c1{vertical-align:top;width:93.6pt;border-style:solid}.c2{vertical-align:top;width:41.8pt;border-style:solid}hr.c1{page-break-before:always}
/*]]>*/
</style>
</head>
<body>
<div>
<p class="c1" style="vertical-align:top;width:93.6pt;border-style:solid"><span>Style C2 Text</span></p>
<hr class="c1" style="page-break-before:always"/>
<p class="c2" style="vertical-align:top;width:41.8pt;border-style:solid">Style C1 Text</p>
</div>
</body>
</html>
Has anybody done this with XSLT before?
Is this also possible with XSLT 1.0 ?Rather than do this in XSLT, which is clunky, and no more portable than Python, I suggest you adapt whatever premailer you prefer to use a straight XML parser instead of a tag soup parser.
Depending on how the premailers work, it might even be possible to use their tag soup parser for a first pass, then just use a DOM or SAX parser for the annotation phase.
精彩评论