Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alan Coleman www.alancoleman.co.uk

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

indexOf - if part of a string contains "Text" for example.

So here, if the string pageName contains "Text" the statement will output positive


   1  
   2  if (pageName.indexOf("Text")!= -1)

Adding a value to an input box with XSL

The XSL attribute has a name that refers to an attribute in the html tag that it follows. In this case, value. The content within the the xsl tags will be assigned to that attribute.

   1  
   2        <input type="text" name="sharesLook" onclick="this.value=''" >
   3        <xsl:attribute name="value">Content</xsl:attribute>
   4  
   5  will output
   6  
   7        <input type="text" value-"Content" name="sharesLook" onclick="this.value=''" >

Simple xsl test position statement for constraining number of records

This statement will constrain the number of records displayed to 4
   1  
   2  <xsl:if test="position() &lt; 4">
   3     Records
   4  </xsl:if>

Simple XSL When statement

Checks to see if 'Stuff' has returned any data. If not then displays 'No data available'. If 'Stuff' has returned data then moves onto otherwise. Use <xsl:when test="stuff = !'' "> to swap the when around.

   1  
   2  <xsl:choose>
   3   <xsl:when test="stuff = '' ">
   4    <div>No data available</div>
   5   </xsl:when>
   6   <xsl:otherwise>
   7    <div><xsl:value-of select="stuff_alternative"/></div>
   8   </xsl:otherwise>
   9  </xsl:choose>

Formatting the time in XSLT

This takes the known variables of hour and min, in 24 hour clock format, and turns it in to 12 hour am/pm format.

   1  
   2        <xsl:choose>
   3          <xsl:when test="hour &lt; 12"><xsl:value-of select="hour" />:<xsl:value-of select="format-number(min,'00')" /> AM</xsl:when>
   4          <xsl:when test="hour = 12"><xsl:value-of select="hour" />:<xsl:value-of select="format-number(min,'00')" /> PM</xsl:when>
   5          <xsl:when test="hour &gt; 12"><xsl:value-of select="hour - 12" />:<xsl:value-of select="format-number(min,'00')" /> PM</xsl:when>
   6        </xsl:choose>

xsl variable with firefox workaround

xsl variable with firefox workaround. This instance takes a previously defined width as ($Width) 255px and applies it to a div.

   1  
   2  <xsl:variable name="x">;width:<xsl:value-of select="($Width) - 10"/>px</xsl:variable>
   3  <!-- same variable for Firefox -->
   4  <xsl:variable name="y">width:<xsl:value-of select="($Width) - 15"/>px!important</xsl:variable>
   5  
   6  <div id="z>
   7   <xsl:attribute name="style">
   8    <xsl:value-of select="$y" />
   9    <xsl:value-of select="$x" />
  10   </xsl:attribute>
  11  </div>
  12  
  13  The resulting HTML will look like this:
  14  
  15  <div id="z" style="width:240px!important;width:245px">

When producing XHTML output with XSL files, you have to use

The method="xml" is needed if you want XHTML valid output, "xhtml" is not a valid parameter.

The encoding="us-ascii" is needed if you want pound, euro, nbsp and any other html entities to show up correctly.

   1  
   2  <xsl:output method="xml" omit-xml-declaration="yes" encoding="us-ascii"/>
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS