首页 > 解决方案 > 根据参数、谓词和当前值设置节点值

问题描述

我当前的 XML 在简化时是这样的 -

<wd:Settlement_Account_Data>
  <wd:Inactive>0</wd:Inactive>
  <wd:Bank_Account_Number>ABCD12345678</wd:Bank_Account_Number>
</wd:Settlement_Account_Data>

在我的 XSLT 中,我有一个参数如下 -

<xsl:param name="changeType"/>

这可以是“A”或“D”,由我的底层程序动态设置。

我想要做的是将“非活动”的值设置为 1 如果

  1. 银行帐号(兄弟节点)以 12345678 结尾。
  2. 不活动当前设置为 0。
  3. 参数设置为'D'

我尝试了以下 3 种方法,但似乎都不起作用。

 <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>

在我的输出中,我似乎总是在条件 #3 下失败,即即使供应商类型为“A”,输出的非活动设置为 1。

以下避免条件 #2 的 XSLT 似乎可以工作 -

   <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>

标签: xmlxsltxslt-2.0

解决方案


我想出了这个 XSLT,基本上运行了你所拥有的 3 个条件(我还删除了前缀 '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>

推荐阅读