XSLT concat字符串,删除最后一个逗号 [英] XSLT concat string, remove last comma

查看:90
本文介绍了XSLT concat字符串,删除最后一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用XSLT构建一个字符串,并用逗号分隔每个字符串,但在最后一个字符串之后不包含逗号。在下面的示例中,如果我具有分发节点而不是注释节点,那么我将使用尾部逗号。无论如何,我都不知道将字符串构建为变量,然后截断XSLT中的最后一个字符。

I need to build up a string using XSLT and separate each string with a comma but not include a comma after the last string. In my example below I will have a trailing comma if I have Distribution node and not a Note node for instance. I don't know of anyway to build up a string as a variable and then truncate the last character in XSLT. Also this is using the Microsoft XSLT engine.

我的字符串=

<xsl:if test="Locality != ''">
  <xsl:value-of select="Locality"/>,
</xsl:if>
<xsl:if test="CollectorAndNumber != ''">
  <xsl:value-of select="CollectorAndNumber"/>,
</xsl:if>
<xsl:if test="Institution != ''">
  <xsl:value-of select="Institution"/>,
</xsl:if>
<xsl:if test="Distribution != ''">
  <xsl:value-of select="Distribution"/>,
</xsl:if>
<xsl:if test="Note != ''">
  <xsl:value-of select="Note"/>
</xsl:if>

[人一定有更好的方法进入此问题文本框:(]

[Man there's gotta be a better way to enter into this question text box :( ]

推荐答案

使用XSLT非常容易完成无需在变量中捕获结果,或使用特殊的命名模板):

I。XSLT 1.0

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/*/*">
      <xsl:for-each select=
      "Locality/text() | CollectorAndNumber/text()
     | Institution/text() | Distribution/text()
     | Note/text()
      "
      >
        <xsl:value-of select="."/>
        <xsl:if test="not(position() = last())">,</xsl:if>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


在以下XML文档上应用此转换时:

when this transformation is applied on the following XML document:

<root>
    <record>
        <Locality>Locality</Locality>
        <CollectorAndNumber>CollectorAndNumber</CollectorAndNumber>
        <Institution>Institution</Institution>
        <Distribution>Distribution</Distribution>
        <Note></Note>
        <OtherStuff>Unimportant</OtherStuff>
    </record>
</root>

产生了想要的结果

Locality,CollectorAndNumber,Institution,Distribution

如果所需元素不是按文档顺序生成的(问题中不是必需的,但由Tomalak提出),实现此目标仍然很容易且优雅:

If the wanted elements should be produced not in document order (something not required in the question, but raised by Tomalak), it is still quite easy and elegant to achieve this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:param name="porderedNames"
     select="' CollectorAndNumber Locality Distribution Institution Note '"/>

    <xsl:template match="/*/*">
        <xsl:for-each select=
         "*[contains($porderedNames, concat(' ',name(), ' '))]">

         <xsl:sort data-type="number"
          select="string-length(
                     substring-before($porderedNames,
                                      concat(' ',name(), ' ')
                                      )
                                )"/>

            <xsl:value-of select="."/>
            <xsl:if test="not(position() = last())">,</xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

此处,字符串参数中提供了所需元素的名称及其所需顺序 $ porderedNames ,其中包含所有所需名称的空格分隔

Here the names of the wanted elements and their wanted order are provided in the string parameter $porderedNames, which contains a space-separated list of all wanted names.

何时上面的转换应用于相同的XML文档,产生了所需的结果

CollectorAndNumber,Locality,Distribution,Institution






II。 XSLT 2.0

在XSLT中,此任务甚至更简单(同样,不需要特殊功能):

In XSLT this task is even simpler (again, no special function is necessary):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/*/*">
    <xsl:value-of separator="," select=
    "(Locality, CollectorAndNumber,
     Institution, Distribution,
     Note)[text()]" />
    </xsl:template>
</xsl:stylesheet>

当此转换应用于同一XML文档时,会产生相同的正确结果

Locality,CollectorAndNumber,Institution,Distribution

请注意,所需元素将以任何所需顺序生成,因为我们使用的是XPath 2.0序列类型(与XSLT 1.0解决方案),根据定义,它包含任何所需(指定)顺序的项目。

Do note that the wanted elements will be produced in any desired order, because we are using the XPath 2.0 sequence type (vs the union in the XSLT 1.0 solution), which by definition contains items in any desired (specified) order.

这篇关于XSLT concat字符串,删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆