RE: [xsl] Variables and Values

Subject: RE: [xsl] Variables and Values
From: "Andreas L. Delmelle" <a_l.delmelle@xxxxxxxxxx>
Date: Tue, 23 Mar 2004 19:15:25 +0100
> -----Original Message-----
> From: Julian Voelcker [mailto:asp@xxxxxxx]
>

Hi,

> I am trying to replicate some programming logic in my template along
> the following lines:
>
> 1. The XML file holds data from a questionnaire.
> 2. At the beginning of a template I want to test to see if a particular
> question has been answered - if it has it will either be true or false
> - if it hasn't been answered there will be no value.

Does this necessarily *have* to take place at the beginning?

Say you have a structure like:

<doc>
  <question nr="1" answer="true" />
  <question nr="2" />
  <question nr="3" answer="true" />
</doc>

If you define two separate templates, like:

<xsl:template match="question[@answer]">
  <xsl:copy-of select="." />
</xsl:template>

<xsl:template match="question[not(@answer)]">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:attribute name="answer">
       <xsl:value-of select="'true'" />
    </xsl:attribute>
  </xsl:copy>
</xsl:template>

Then the following instruction:

<xsl:apply-templates select="doc/question" />

will do what is necessary. The questions having an answer attribute will
just be copied, the questions without an answer attribute will be copied to
the result tree with addition of an answer attribute set to 'true'.


Hope this helps!

Cheers,

Andreas

Current Thread