Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

xml - Set node value based on a parameter, a predicate and the current value

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

  1. The bank account number (sibling node) ends with 12345678.
  2. Inactive is currently set to 0.
  3. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

not sure if this is helpful, I came up with this XSLT, basically running through the 3 conditions you have (I also removed the prefix 'wd'):

<xsl:template match="/Settlement_Account_Data/Inactive">
        <xsl:choose>
            <xsl:when test="$changeType = 'D'">
                <Inactive>1</Inactive>
            </xsl:when>
            <xsl:when test="ends-with(parent::Settlement_Account_Data/Bank_Account_Number,'12345678')">
                <Inactive>1</Inactive>
            </xsl:when>
            <xsl:otherwise>
                <Inactive>
                    <xsl:value-of select="."/>
                </Inactive>
            </xsl:otherwise>
        </xsl:choose>      
    </xsl:template>

I hope that is helpful.

Best regards, Peter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...