CSS space in name & troublesome XSLT
I'm trying to create an XSLT which lists out movies, and I'm styling the titles so that each title has it's own color, I then select the tit开发者_StackOverflowle with with interpolation (XPath?)
Here is the XSL file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Videos</title>
<style>
table, tr, td { border: thin black solid }
th { background-color: #AAFFAA }
.Daredevil { color: red; }
/* Look at those spaces! Hrmphf! */
.Drag Me To Hell { color: green; }
</style>
</head>
<body>
<table>
<tr><th>Movies</th></tr>
<xsl:apply-templates select="//movie"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="//movie[not(@title=preceding::movie/@title)]">
<!-- Title should be the one of the titles in the CSS -->
<tr class="{@title}"><td><xsl:value-of select="@title"/></td></tr>
</xsl:template>
</xsl:stylesheet>
The problem however is that some movie titles contain spaces. Can I have spaces in the name? If not, are there any workarounds (besides having underscores or the like in the XML)?
Update:
Ok, I get that it's not possible. At the moment I'm using translate(@title, ' ', '-')
Which works when the CSS-names are delimited by -
. I would still like to know if there are any better way of doing this. :)
class="Drag me to"
creates an element with three different classes.
The selector .Drag.Me.To
will match elements with all three classes.
However, it will match them in any order, and won't count duplicates.
No, you cannot have spaces in CSS rule names
CSS classes / id´s cannot contain spaces.
Since a space is used to seperate different classes.
You will have to resolve to a (-)
It feels a bit odd to me to tie the CSS to the actual data content in this way. Your stylesheet seems to know in advance what it expects to find in the data file, and that feels like bad design - it should be generic. Why not introduce an indirection from movie title to CSS classes:
<xsl:variable name="movieStyles">
<movie title="Drag me to hell" css-class="hell-class"/>
<movie ...
</xsl:variable>
then <tr class="{f:css-class-for-title(@title)}"
...
where the function f:css-class-for-title uses the lookup data to map movie titles to CSS classes.
精彩评论