My current XML is something like this when simplified -
<wd:Settlement_Account_Data>
<wd:Inactive>0</wd:Inactive>
<wd:Bank_Account_Number>ABCD12345678</wd:Bank_Account_Number>
</wd:Settlement_Account_Data>
In my XSLT, I have a parameter as follows -
<xsl:param name="changeType"/>
This can be either 'A' or 'D', and is set dynamically by my underlying program.
What I want to do is set the value of "Inactive" as 1 if
- The bank account number (sibling node) ends with 12345678.
- Inactive is currently set to 0.
- The parameter is set as 'D'
I tried the following 3 approaches, and none of them seem to work.
<xsl:template match="wd:Settlement_Account_Data/wd:Inactive[ends-with(../wd:Bank_Account_Number, '12345678')]">
<xsl:choose>
<xsl:when test="$changeType = 'D'"><wd:Inactive>1</wd:Inactive></xsl:when>
<xsl:otherwise><wd:Inactive><xsl:value-of select="."/></wd:Inactive></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="wd:Settlement_Account_Data/wd:Inactive[ends-with(../wd:Bank_Account_Number, '12345678')]/text()">
<xsl:choose>
<xsl:when test="$changeType = 'D'">1</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="wd:Settlement_Account_Data/wd:Inactive[ends-with(../wd:Bank_Account_Number, '12345678')][.='0']/text()">
<xsl:choose>
<xsl:when test="$changeType = 'D'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
In my output, I always seem to be failing with condition #3, i.e., the output has inactive set to 1 even if the supplier type is 'A'.
The following XSLT which avoids condition #2 seems to work though -
<xsl:template match="wd:Settlement_Account_Data/wd:Inactive[ends-with(../wd:Bank_Account_Number, '12345678')]/text()">
<xsl:choose>
<xsl:when test="$changeType = 'D'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
Any insight on the correct approach would be helpful.
question from:
https://stackoverflow.com/questions/66052657/set-node-value-based-on-a-parameter-a-predicate-and-the-current-value 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…