根据三根弦的存在将它们连接起来

  • 本文关键字:起来 连接 存在 三根 xslt
  • 更新时间 :
  • 英文 :


我需要将 3 个字符串与","分隔符连接起来:

I/P XML:

<Data>
<First>XXX</First>
<Second>YYY</Second>
<Third>ZZZ</Third>
</Data>

预期应付账款:

如果所有元素都有数据,则<Concatinate>XXX,YYY,ZZZ</Concatinate>

或者如果第二个元素为空,则<Concatinate>XXX,ZZZ</Concatinate>

或者如果第三个元素为空,则

<Concatinate>XXX,YYY</Concatinate>

我怎样才能做到这一点,请建议。

问候 毗湿奴。

XSLT 1.0

<xsl:template match="Data">
<Concatinate>
<!-- Matching all Child that is not empty -->
<xsl:for-each select="child::*[normalize-space()]">
<xsl:value-of select="."/>
<!-- below code is use to put comma if position is not last -->
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</Concatinate>
</xsl:template>

XSLT 2.0

<xsl:template match="Data">
<Concatinate><xsl:value-of select="First[node()]|Second[node()]|Third[node()]" separator=","/></Concatinate>
</xsl:template>

同样在XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<Concatinate><xsl:apply-templates/></Concatinate>
</xsl:template>
<!-- Template to handle Child Element  -->
<xsl:template match="First[node()]|Second[node()]|Third[node()]">
<xsl:if test="preceding-sibling::*[node()]"><xsl:text>,</xsl:text></xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet

最新更新