删除XSLT中的最后一个逗号

删除XSLT中的最后一个逗号,xslt,xslt-1.0,Xslt,Xslt 1.0,使用XSLT1.0 <xsl:for-each select="*"> <xsl:variable name="xxxx" select="@name" /> <xsl:if test="../../../../fieldMap/field[@name=$xxxx]">... <xsl:if test="position() != last()">////this is not work correctly as l

使用
XSLT1.0

<xsl:for-each select="*">
    <xsl:variable name="xxxx" select="@name" />
    <xsl:if test="../../../../fieldMap/field[@name=$xxxx]">...
        <xsl:if test="position() != last()">////this is not work correctly as last() number is actual last value of for loop and position() is based on if condition.
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:if>
</xsl:for-each>

...
////这不能正常工作,因为last()number是for循环的实际最后一个值,position()基于if条件。
,

您能建议我如何删除最后一个“
”吗?

您可以将您的内部
更改为:

    <xsl:if test="not(following-sibling::*[
                   @name = ../../../../fieldMap/field/@name])">
        <xsl:text>,</xsl:text>
    </xsl:if>
如果A选择的任何节点与B选择的任何节点相等(具有相同的值),则为true

为了方便起见,我可能会将
。/../../../../fieldMap/field/@name
放入一个变量中,并在
For each
循环开始之前声明它:

<xsl:variable name="fieldNames" select="../../../../fieldMap/field/@name" />
<xsl:for-each select="*">
    <xsl:if test="$fieldNames = @name">...
        <xsl:if test="not(following-sibling::*[@name = $fieldNames])">
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:if>
</xsl:for-each>

...
,
同样,$fieldNames可以是多属性节点的节点集,当我们说
$fieldNames=@name
时,我们要问的是
@name
的值是否等于
$fieldNames
position()
last()
中任何节点的值都应该基于循环,而不是
xsl:if
。我认为您所说的是,您实际上想要检查这是否是xsl:if
为true的最后一个元素,因为这样的元素实际上可能不是循环中的最后一个元素

我建议将
xsl:for each
xsl:if
组合成一个,并只选择条件为true的元素。这样,您就可以按照预期的方式检查位置

<xsl:for-each select="*[@name = ../../../../fieldMap/field/@name]>

    <xsl:if test="position() != last()">
         <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:for-each>

翻译、替换、拆分连接在XSLT中不起作用。我实现了一种简单的方法,这种方法在XSLT中运行良好

示例代码:

  <xsl:variable name="ErrorCPN">
    <xsl:if test="count(/DATA_DS/G_1)>0 and count(/DATA_DS/CPN)>0">
      <xsl:for-each select="$BIPReportCPN/ns3:BIPCPN/ns3:BIPEachCPNDelimitedValue">
        <xsl:variable name="BIPEachCPNDelimitedValue" select="."/>
        <xsl:if test="count(/DATA_DS/G_1[CPN=$BIPEachCPNDelimitedValue]/CPN)=0">
          <xsl:value-of select="concat($BIPEachCPNDelimitedValue,',')"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:variable>
  <xsl:value-of select="substring($ErrorCPN,1,string-length($ErrorCPN)-1)"/>
</xsl:if> 

我创建了一个变量,对于实现的每个条件,在if和for内部,对于每个循环所需的值都用逗号连接,在循环的末尾会有一个额外的逗号,这对我们来说是不需要的。因此,我们可以取子字符串并删除最后一个逗号


否则请使用示例代码并重试。

您能给我们一些示例输入和输出数据吗?我不认为“position()是基于if条件的”;或者我不明白你的意思。这里,我有fieldMap/field/@name是多个field/@name元素,还有其他比较元素是多个元素*/@name。在for条件下,它会像*/@name与../../../fieldMap/field/@name的所有匹配一样工作吗?那么,为什么不试试并找出答案!;)或者,您可以用这些信息编辑您的问题,理想情况下显示您的输入XML和预期输出,因为这样会使问题更清楚。非常感谢。抱歉,回复有点晚。是的,它适用于我,它与所有元素集进行比较。它的例子也是,在其他的岗位上。非常感谢。
  <xsl:variable name="ErrorCPN">
    <xsl:if test="count(/DATA_DS/G_1)>0 and count(/DATA_DS/CPN)>0">
      <xsl:for-each select="$BIPReportCPN/ns3:BIPCPN/ns3:BIPEachCPNDelimitedValue">
        <xsl:variable name="BIPEachCPNDelimitedValue" select="."/>
        <xsl:if test="count(/DATA_DS/G_1[CPN=$BIPEachCPNDelimitedValue]/CPN)=0">
          <xsl:value-of select="concat($BIPEachCPNDelimitedValue,',')"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:variable>
  <xsl:value-of select="substring($ErrorCPN,1,string-length($ErrorCPN)-1)"/>
</xsl:if>