processing css stylesheet with xml document -
i new xml (a couple of days now...)and going good, however, can't seem xml processed css style
here perl...
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use text::csv_xs; $csv = text::csv_xs->new ({ binary => 1, auto_diag => 1 }); $ifile="elementarray.csv"; $ofile="elementarray.xml"; open $fh, "<", $ifile or die $ifile.": $!"; open $out, "> ".$ofile or die "cannot write ".$ofile.": $!"; print $out <<eot; <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="elementarray.xsl"?> <?xml-stylesheet href="elementarray.css" title="default style"?> <emailcomments> eot # first record contains list of fieldnames #my $fields = $csv->getline($fh); #print @$fields; while (my $row = $csv->getline($fh)) { last unless $row && @$row; # encode "<" characters "<" , "&" "&" in fields foreach (@$row) { s/&/&/g; s/</</g; } # create hash of fields using hash slice %row; @row{"subject","body","from: (name)","from: (address)","to: (name)","to:(address)","date","time"} = @$row; print $out <<eot; <email> <header> <origname>$row{"from: (name)"}</origname> <origaddress>$row{"from: (address)"}</origaddress> <destname>$row{"to: (name)"}</destname> <destaddress>$row{"to: (address)"}</destaddress> <subject>$row{"subject"}</subject> </header> <body>$row{"body"}</body> <date id="date">$row{"date"}</date> <time id="time">$row{"time"}</time> </email> eot } print $out "</emailcomments>\n"; $csv->eof or $csv->error_diag; close $fh or die $ifile.": $!"; close $out or die $ofile.": $!";
here xsl...
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output indent="yes" method="html"/> <xsl:template match="/"><!-- 'pattern', / matches root node --> <html> <head> <title>e-mail</title> </head> <body> <xsl:apply-templates/><!-- instruction --> </body> </html> </xsl:template> <xsl:template match="header"> <span style="color:red; font-weight:bold; font-style:italic"> <xsl:value-of select="subject"/> </span> from: <xsl:call-template name="formatemail"> <xsl:with-param name="address" select="origaddress"/> </xsl:call-template> to: <xsl:call-template name="formatemail"> <xsl:with-param name="address" select="destaddress"/> </xsl:call-template> </xsl:template> <xsl:template match="email"> <xsl:apply-templates select="header"/> <pre> <xsl:value-of select="body"/> </pre> <div> <span> <xsl:attribute name="id"><xsl:value-of select="date"/></xsl:attribute> <!-- --><xsl:value-of select="date"/><!-- --></span> <span> <xsl:attribute name="id"><xsl:value-of select="time"/></xsl:attribute> <!-- --><xsl:value-of select="time"/><!-- --></span> </div> <br /> </xsl:template> <xsl:template name="formatemail"> <xsl:param name="address"/> <a> <xsl:attribute name="href"><xsl:value-of select="concat('mailto:',$address)"/></xsl:attribute> <xsl:value-of select="$address"/> </a> </xsl:template> </xsl:stylesheet>
here xml (1 record)...
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="elementarray.xsl"?> <?xml-stylesheet href="elementarray.css" title="default style"?> <emailcomments> <email> <header> <origname>william holt</origname> <origaddress><x@gmail.com></origaddress> <destname>x</destname> <destaddress>bill@elementarray.com</destaddress> <subject>welcome neighborhood</subject> </header> <body>just thought i'd hello</body> <date id="date">07/18/2013</date> <time id="time">11:53</time> </email> </emailcomments>
and here css ( style see if working, , isn't :( )...
date{background-color:red;} time{background-color:green;} #date{background-color:yellow;} #time{background-color:blue;} body{background-color:grey;}
sorry length of post think need these files...
although can indeed style xml css, using xml-stylesheet
processing instruction have included in xml, can't transform xslt @ same time. should have 1 stylesheet processing instruction in xml. either style xml css (which not common thing do), or transform html xslt , style html output css.
in latter case, remove <?xml-stylesheet href="elementarray.css"?>
instruction xml document, , instead output reference css document in first template.
<xsl:template match="/"> <html> <head> <title>e-mail</title> <link href="elementarray.css" type="text/css" rel="stylesheet" /> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template>
now, in css, see have selectors date , time ids, need ensure relevant elements in html have these ids.
at moment, doing in xslt
<span> <xsl:attribute name="id"><xsl:value-of select="date"/></xsl:attribute> <!-- --><xsl:value-of select="date"/><!-- --></span> <span>
but setting id attribute have value of date element in xml, such element not exist! think meaning use literal string here. now, this...
<xsl:attribute name="id"><xsl:value-of select="'date'"/></xsl:attribute>
but verbose. can this:
<xsl:attribute name="id">date</xsl:attribute>
but better still, write out attribute in normal way:
<span id="date"> <xsl:value-of select="date"/> </span>
and 'time'. should ensure 2 span tags styled using css in html output.
Comments
Post a Comment