views:

54

answers:

2

I am looping thru looking at the values of a Node.

If Node = B, then B has one of two possible meanings. 
  --If Node = A has been previously found in the file, then the value for A
    should be sent as 1.
  --If Node = A has NOT been found in the file, the the value for A should 
    be sent as 2.

where file is the xml source to be transformed

I cannot figure out how to do this. If I was using a programming language that allowed for a variable to have its value reassigned/changed, then it is easy. But, with XSLT variables are set once.

A: 

Look into xsl:choose, xsl:when, xsl:if. You can do

<xsl:if test="A=1">
Set in here
</xsl:if>

<xsl:choose>
    <xsl:when test="A=1">
        <xsl:otherwise>
</xsl:choose>
Rob
Thanks for answer, and intro to choose. but I think I did not explain my problem clearly. Have posted another question:XSLT: For each node transform, if A =2 and A=1 were both found do this else do that
Larry
A: 

The code you provide has nothing to do with XSLT at all. Please, read a good book on XSLT before asking such questions.

Here is the very well known way of doing what I guess is in the meaning of your question:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="/">
   <xsl:variable name="vA">
     <xsl:choose>
      <xsl:when test="//B">1</xsl:when>
      <xsl:otherwise>2</xsl:otherwise>
     </xsl:choose>
   </xsl:variable>

   $vA = <xsl:value-of select="$vA"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<c>
  <d/>
</c>

the result is:

   $vA = 2

When applied on this document:

<c>
  <d>
   <B/>
  </d>
</c>

the reult is:

   $vA = 1

There is a shorter way to obtain the same result:

  <xsl:variable name="vA" select="not(//B) +1"/>
Dimitre Novatchev
Thanks for answer, and your example. I will keep it in mind for the future. but I think I did not explain my problem clearly. Have posted another question:XSLT: For each node transform, if A =2 and A=1 were both found do this else do that
Larry