This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sorting


Hi Krithiga,

> I output the HTML to loop thru each x.

I take it you mean you're using xsl:for-each to iterate:

<xsl:for-each select="x">
   ...
</xsl:for-each>

> But i have to create a text box if the value of col in x is < 2 ,
> i.e only in case where x has id=2 and only 1 col

I'm a bit confused about what you want to test exactly. You want to
test whether the value of the 'col' element (which is a child of 'x'
is less than 2. Within the xsl:for-each, the current node is the 'x'
element. So, to get the value of its col child, you need the XPath:

  child::col

or, as child:: is the default axis, simply:

  col

To test whether this is less than 2, you can use:

  col &lt; 2

This tests whether *any* of the col children of the x element have a
value less than 2.  If you want to test whether *all* of the col
children of the x element have values less than 2, then you have to
turn it around and ask whether it's *not* the case that *any* of the
col children of the x element have values *greater than or equal to*
2:

  not(col &gt;= 2)

If you want to test how many col children an 'x' element has, then use
the count() function to count them:

  count(col)

So if you want to see whether it only has one col, then use:

  count(col) = 1

If you want to test whether the id attribute of the 'x' element is
equal to '2', then use:

  @id = 2

You can combine the tests together with 'and' and 'or'.  For example,
if you want to test whether there's only one col and that col has a
value less than 2, then use:

  count(col) = 1 and col &lt; 2

To do something conditionally within the xsl:for-each, and given that
you'll be doing something else with other 'x' elements, you need
xsl:choose:

  <xsl:choose>
    <xsl:when test="...your condition...">
      <!-- create the text box -->
    </xsl:when>
    <xsl:otherwise>
      <!-- do whatever you do usually -->
    </xsl:otherwise>
  </xsl:choose>

An alternative is to use xsl:apply-templates rather than xsl:for-each
and have templates for the different kinds of 'x' elements:

  <xsl:apply-templates select="x" />

... later in the stylesheet ...

<xsl:template match="x">
  <!-- do whatever you do usually -->
</xsl:template>

<xsl:template match="x[...your condition...]">
  <!-- create the text box -->
</xsl:template>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]