XSLT 1.0 node-set函数替代方案(适配不支持该函数的SAP系统)
解决方案
完全可以不使用node-set()扩展函数实现相同功能,核心思路是调整递归分割逻辑,在拆分字符串的同时直接输出目标节点,无需先将拆分结果存入临时变量再转换为节点集遍历,纯XSLT 1.0标准语法即可兼容SAP XSLT处理器。
修改后完整XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="http://siemens.com/ad/geb/cartmanagement/1.2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="i doc"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/doc:Input_DT"> <!-- 直接调用分割模板,无需中间变量存储拆分结果 --> <xsl:call-template name="tokenize"> <xsl:with-param name="text" select="doc:Description"/> <xsl:with-param name="position" select="1"/> </xsl:call-template> </xsl:template> <xsl:template name="tokenize"> <xsl:param name="text"/> <xsl:param name="delimiter" select="','"/> <!-- 新增位置计数参数,每次递归自动累加 --> <xsl:param name="position"/> <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" /> <xsl:if test="$token"> <!-- 直接输出目标节点,无需封装为Number节点 --> <pos> <xsl:value-of select="$position"/> </pos> <printtokValue> <!-- 新增normalize-space处理分割后字段前后的多余空格 --> <xsl:value-of select="normalize-space($token)"/> </printtokValue> </xsl:if> <xsl:if test="contains($text, $delimiter)"> <!-- 递归调用时位置计数+1 --> <xsl:call-template name="tokenize"> <xsl:with-param name="text" select="substring-after($text, $delimiter)"/> <xsl:with-param name="position" select="$position + 1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
关键修改说明
- 移除了所有
exsl/ext扩展命名空间、扩展前缀声明,完全不依赖扩展函数 - 给递归分割模板新增
position计数参数,每次递归时自动累加,替代原来通过node-set遍历获取position()的逻辑 - 分割出每个子串时直接输出
<pos>和<printtokValue>节点,省去了临时存储拆分结果、再用node-set转换遍历的中间步骤 - 增加
normalize-space()处理,自动去掉分割后子串前后的多余空格,输出结果更整洁
输出效果
和原代码的输出完全一致,针对你提供的输入XML,输出如下:
<pos>1</pos> <printtokValue>RCBO</printtokValue> <pos>2</pos> <printtokValue>10 kA</printtokValue> <pos>3</pos> <printtokValue>4P</printtokValue> <pos>4</pos> <printtokValue>type AC</printtokValue> <pos>5</pos> <printtokValue>30 mA</printtokValue> <pos>6</pos> <printtokValue>C-Char</printtokValue> <pos>7</pos> <printtokValue>In: 125 A</printtokValue> <pos>8</pos> <printtokValue>Un AC: 400 V</printtokValue>
内容的提问来源于stack exchange,提问作者Sateesh
相关产品推荐
相关产品推荐

