XSLT: If Node = A then set B=1 Else B=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.
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">
<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"/>
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>
精彩评论