Push Button Paradise
Micah Dubinko
Fri, 13 May 2005
Parsing CSS in XSLT 2.0
If you need to take apart CSS, say a style attribute, XSLT 2.0 is a
pretty handy tool.
<xsl:variable name="rules" select="tokenize(replace(@style, '( )|(;$)', ''), ';')"/>
The replace call gets rid of any spaces, as well as a trailing semicolon,
should one exist. Next, the tokenize call splits up the rules at each
semicolon. Since there's no trailing semicolon, you don't get an empty string at
the end of the resulting sequence.
I'm fortunate enough to be working with regular, fairly simple CSS. Note that
this will fail in more complex cases with quoted values like font names containing
spaces, or compound values like border: 42px outset teal. -m
P.S. I've been looking at examples like these, except better written, from Mike Kay et.al. in xsl-list for more than a year now, and things still haven't soaked in--until I started writing code with it.
Update: I was asking around on xsl-list, and Mike Kay himself clued me in to a whole new section of XSLT 2.0 that I wasn't even aware existed: analyze-text. Wow. How long until someone writes Text Processing in XSLT?
posted at: 22:55 | under: 2005-05 | 0 comment(s)

