views:

197

answers:

2

Hi,

I'm using Xsl transformation to display an Xml data as Html.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ms="urn:schemas-microsoft-com:xslt"
 xmlns:util="urn:the-xml-files:xslt">
 <xsl:output method="xml" indent="yes"
  omit-xml-declaration="yes" encoding="utf-8"/>

 <xsl:template match="/">
     <xsl:for-each select="/Categories/Category">
       <li class="c">
         <a class="d">
          <xsl:attribute name="id">cat_<xsl:value-of select="categoryid"/></xsl:attribute>
         </a>
       </li>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

If the li element's id attribute's select is not in one line, the XSL processor will fill up the attribute value with whitespace, which totally breaks the javascript on the front end.

Of course, Visual Studio will always reformat many-definitions-in-one-line, so if I change something, I need to remove the whitespace manually.

How can I remove unnecessary whitespace from an element? Should I do an xsl:copy with xsl:strip-space, or is there any better solutions? :)

+3  A: 

The solution is simple:

Instead of:

     <a class="d">
      <xsl:attribute name="id">cat_<xsl:value-of select="categoryid"/></xsl:attribute>
     </a>

Use:

     <a class="d" id="cat_{categoryid}"/>
Dimitre Novatchev
Dimitre, where can I find more info about this short mode?
balint
@balint It is called AVT (Attribute-Value-templates) and is defined here: http://www.w3.org/TR/xslt#attribute-value-templates
Dimitre Novatchev
+2  A: 

In addition to Dimitre's elegant solution, the following verbose version is available:

<a class="d">
   <xsl:attribute name="id">
      <xsl:text>cat_</xsl:text>
      <xsl:value-of select="categoryid"/>
   </xsl:attribute>
</a>

I only mention this because the <xsl:text> is often overlooked and can be quite useful. XSL is a verbose language anyway.

Ishmael