Firstly, there is nothing wrong with spaces in attribute values โโin XML: roughly speaking, normalizing an attribute value converts whitespace to spaces and folds adjacent spaces into a single space when the document is parsed, but the space is definitely resolved. EDIT: See below for more on this.
Matthew Wilson's approach does not include spaces between possible meanings, as you mentioned in your comments on it. However, his approach is fundamentally sound. The final piece of the puzzle is your dislike of redundant spaces: they can be eliminated using the normalize-space XPath function.
The following stylesheet combines all the bits - note that it does nothing with its source document, so for testing purposes you can run it against any XML document or even yourself to make sure that the result meets your requirements.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="foo0" select="false()"/> <xsl:variable name="bar0" select="true()"/> <xsl:variable name="foo1" select="true()"/> <xsl:variable name="bar1" select="false()"/> <xsl:variable name="foo2" select="true()"/> <xsl:variable name="bar2" select="true()"/> <xsl:template match="/"> <xsl:variable name="foobar0"> <xsl:if test="$foo0"> foo</xsl:if> <xsl:if test="$bar0"> bar</xsl:if> </xsl:variable> <xsl:variable name="foobar1"> <xsl:if test="$foo1"> foo</xsl:if> <xsl:if test="$bar1"> bar</xsl:if> </xsl:variable> <xsl:variable name="foobar2"> <xsl:if test="$foo2"> foo</xsl:if> <xsl:if test="$bar2"> bar</xsl:if> </xsl:variable> <li> <xsl:attribute name="class"> <xsl:value-of select="normalize-space($foobar0)"/> </xsl:attribute> </li> <li> <xsl:attribute name="class"> <xsl:value-of select="normalize-space($foobar1)"/> </xsl:attribute> </li> <li> <xsl:attribute name="class"> <xsl:value-of select="normalize-space($foobar2)"/> </xsl:attribute> </li> </xsl:template> </xsl:stylesheet>
EDIT: In addition to the issue of spaces separating discrete components in an attribute value: XML Spec defines a number of possible valid constructs as attribute types , including IDREFS and NMTOKENS. The first case corresponds to Names , and the second case corresponds to NMTokens production; both of these settings are defined as containing several values โโof the corresponding type, limited by spaces. Thus, lists of values โโseparated by spaces, as the value of a single attribute, are an integral component of the XML dataset.
Nickfitz
source share