views:

25

answers:

2

I have an xslt document and I want to output an anchor (a) tag with some attributes who's values depend on other things.. Thus, I use the xsl:attribute tag with a choose/if underneath it (or vice-versa).. So my code looks like this:

<a href="/somepage.html">
  <xsl:if test="current_page='this_page'">
   <xsl:attribute name='class'>active</xsl:attribute>
  </xsl:if>
My Page
</a>

However the problem is then in the output html all the newlines/spaces are there which ends up making my link have an extra space to the left of it (and its underlined making it obvious). So the obvious solution is to do this:

<a href="/somepage.html"><xsl:if test="current_page='this_page'"><xsl:attribute name='class'>active</xsl:attribute></xsl:if>My Page</a>

to get rid of the space.. Not too big of a deal in the above code but my actual page has a lot more logic to it making this really ugly.. The only other thing I can think of to clean this up is to put the logic outside the link generation but then I'm repeating things moreand having to create more variables. Which is reasonable but still not totally ideal. This is just one example where I've wanted to do this, its happened several other times so I was just wondering if there are any other ways of solving this.

+3  A: 

Maybe you could use this in the beginning of the XSLT document:

<xsl:strip-space elements="a"/>

Update, this works:

<a href="/somepage.html">
  <xsl:if test="current_page='this_page'">
   <xsl:attribute name='class'>active</xsl:attribute>
  </xsl:if>
  <xsl:text>My Page</xsl:text>
</a>
lasseespeholt
this strips whitespace from a elements in the input.. in this case I'm actually generating the a tag I just want to do so without space in the innerhtml
Matt Wolfe
I see :/ Try with `xsl:text` as in the example. It works :)
lasseespeholt
@lasseespeholt: +1 For correct answer.
Alejandro
A: 

Would a simple xsl:strip-space at the top of you style sheet be sufficient?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:output method="html" indent="yes"/>

  <xsl:strip-space elements="*"/>

  ...

</xsl:stylesheet>
0xA3