Re: CSS in XML Documents

I received a private response to this asking for more info, so here goes.

The best way to show what I'm doing is with an example:
  Suppose you have the following CSS...

p               {text-indent:0.3in; margin-top:0pt; margin-bottom:0pt; 
font-size:13p
p.just  {text-align:justify}
p.center        {text-align: center}
.greek  {font-family:'SIL Galatia'}
h1, h2, h3      {text-align: center;}
a:link     {color: green;}

  In XCSS, that becomes...

<style type="text/xcss">
<selector element="p" class="" pseudo="">
  <property name="text-indent" value="0.3in"/>
  <property name="margin-top" value="0pt"/>
  <property name="margin-bottom" value="0pt"/>
  <property name="font-size" value="13pt"/>
</selector>
<selector element="p" class="just" pseudo="">
  <property name="text-align" value="justify"/>
</selector>
<selector element="p" class="center" pseudo="">
  <property name="text-align" value="center"/>
</selector>
<selector element="" class="greek" pseudo="">
  <property name="font-family" value="'SIL Galatia'"/>
</selector>
<selector element="h1" class="" pseudo="">
  <property name="text-align" value="center"/>
</selector>
<selector element="h2" class="" pseudo="">
  <property name="text-align" value="center"/>
</selector>
<selector element="h3" class="" pseudo="">
  <property name="text-align" value="center"/>
</selector>
<selector element="a" class="" pseudo="link">
  <property name="color" value="green"/>
</selector>
</style>

  This was auto-generated by a Perl script that converts CSS to XCSS. 
Now then, the interesting part. When I need to transform this to CSS 
(for use in HTML, Open EBook, etc.), I apply the following XSLT template 
to it...

  <xsl:template match="style[@type = 'text/xcss']">
    <xsl:for-each select="selector">
      <xsl:value-of select="@element"/><xsl:if test="@class != 
''">.<xsl:value-of select="@class"/></xsl:if><xsl:if test="@pseudo != 
''">:<xsl:value-of select="@pseudo"/></xsl:if><xsl:text> { 
</xsl:text><xsl:for-each select="property"><xsl:value-of 
select="@name"/>: <xsl:value-of select="@value"/>; 
</xsl:for-each><xsl:text>}&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>

That results in plain old CSS. Now, if I am transformming the doc to 
XSL-FO, then CSS won't work, so I just put this inside the templates 
that handle elements that can have CSS properties...

   <fo:block id="{generate-id(.)}">
      <xsl:for-each select="//style[@type = 
'text/xcss']/selector[(@element = local-name(current()) or @element = 
'') and (@class = current()/@class or @class = '')]/property">
        <xsl:attribute name="{@name}"><xsl:value-of 
select="@value"/></xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </fo:block>

And of course, that's just an example, you can apply that for-each loop 
to <fo:inline/> as well, where appropriate. In XSL-FO, is there any way 
to collect style information into a central place like the <style/> 
element does for HTML? I couldn't find one, so I am putting the above 
into just about every template in my FO stylesheet.

It's a little clunky, but it does work pretty well. One caveat is that 
you kind of have to restrict yourself to the intersection of CSS and 
XSL-FO properties for everything to transform correctly. I am probably 
going to do some conversion in the future, however. Like changing the 
CSS values "left" and "right" to XSL-FO's "start" and "end" where 
appropriate.

It seems as though this is something which would be useful to others. Is 
the W3C interested in creating a standard that encompasses this idea? 
The ability to transform style information with XSLT seems pretty useful.

Wes Morgan

Received on Monday, 30 July 2001 10:36:25 UTC