Question

I have an xslt document and I want to output an anchor (a) tag with some attributes whose 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 it's 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 more and 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.

Was it helpful?

Solution

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>

OTHER TIPS

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">

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

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

  ...

</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top