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

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

问题描述

我需要使用 XSLT 构建一个字符串并用逗号分隔每个字符串,但在最后一个字符串后不包括逗号.在下面的示例中,如果我有 Distribution 节点而不是 Note 节点,我将有一个尾随逗号.我不知道无论如何要建立一个字符串作为变量,然后截断 XSLT 中的最后一个字符.这也是使用 Microsoft 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 很容易实现(无需在变量中捕获结果,或者使用特殊的命名模板):

我.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

<小时>

二.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 连接字符串,删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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