Re: [xsl] Test for PCDATA

Subject: Re: [xsl] Test for PCDATA
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Feb 2001 22:35:56 +0000
Hi Marc,

> I need to be able to test for PCDATA from an element based on part of
> the string.
>
> If is have PCDATA such as TOC_RCVD, TOC_SENT, REQ_RCVD,REQ_SENT....I
> need to have a test that determines something such as *_RCVD or
> *_SENT and then apply the appropriate style.

You need to use the string manipulation functions like substring() and
perhaps substring-after().  Let's say that the element you're testing
is something like:

  <foo>TOC_RCVD</foo>

You could test whether the 'foo' element ends with _RCVD with:

  substring(foo, string-length(foo) - 4) = '_RCVD'

Alternatively, you could test whether the part of the string after the
underscore ('_') equals 'RCVD' with:

  substring-after(foo, '_') = 'RCVD'

If you want to do a certain thing to foo elements for which this is
the case, then you can create a template that matches only those foo
elements and does something special to them:

<xsl:template match="foo[substring-after(., '_') = 'RCVD']">
   <!-- RCVD foo -->
   ...
</xsl:template>

If what you do to RCVD and SENT foo elements is largely the same, then
it's probably better to bung the code all in one foo-matching
template, and test within it just to change the bits that differ:

<xsl:template match="foo">
   <xsl:choose>
      <xsl:when test="substring-after(., '_') = 'RCVD'">
         <!-- RCVD-specific stuff -->
      </xsl:when>
      <xsl:when test="substring-after(., '_') = 'SENT'">
         <!-- SENT-specific stuff -->
      </xsl:when>
   </xsl:choose>
   <!-- general stuff -->
</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


Current Thread