xml - 根据参数、谓词和当前值设置节点值
问题描述
我当前的 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 如果
- 银行帐号(兄弟节点)以 12345678 结尾。
- 不活动当前设置为 0。
- 参数设置为'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>
解决方案
我想出了这个 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>
推荐阅读
- c++ - 是否可以传递结构的实例并更改结构的属性
- python-3.x - 如何从作为变量的一部分的字符串中引用类 3.7
- c# - 为什么 Xamarin 上的 oneSignal 的玩家 ID 为空?仅在 iOS 中
- python - 如何获取与 kafka-python 中的主题相关联的消费者组列表?
- sql - 根据条件将表中的数据拆分为 2 列
- xpath - 基于元素部分匹配拉节点的xpath
- swift - instanceViewController 在单元测试中抛出 SIGABRT
- android - 如何在 Android 中以编程方式创建文件夹
- javascript - 遍历 JSON 并找到匹配的名称以返回 ID
- powershell - Powershell 无法访问对象属性字段和值