开发者

How to not output whitespace in xslt for an element that has attributes

开发者 https://www.devze.com 2023-01-18 06:46 出处:网络
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-vers

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'&g开发者_Python百科t;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.


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>


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号