【问题标题】:How use more than one value for attribute in XSLT如何在 XSLT 中为属性使用多个值
【发布时间】:2019-07-22 07:47:10
【问题描述】:

我想为 XSLT 中的属性使用多个值。

输入:

<contrib contrib-type="author">
     <name>
         <surname>Khorana</surname>
         <given-names>Alok A.</given-names>
     </name>
     <degrees>MD</degrees>
</contrib>
<contrib contrib-type="author">
     <name>
         <surname>Holand</surname>
         <given-names>Gamak J.</given-names>
     </name>
     <degrees>PhD</degrees>
</contrib>

输出应该是:

<fieldSet name="Author" value="Alok A. Khorana, MD Gamak J. Holand, PhD"/>

尝试过的代码:

<xsl:template name="take-author">
    <tps:fieldSet name="Author">
        <xsl:attribute name="value">
            <xsl:value-of select="concat(descendant::contrib[@contrib-type='author']/name/given-names,descendant::contrib[@contrib-type='author']/name/surname)"/>
        </xsl:attribute>
    </tps:fieldSet>
</xsl:template>

但在尝试上述代码时出现以下错误

第一个参数不允许包含多个项目的序列 的 concat()

【问题讨论】:

    标签: xslt


    【解决方案1】:

    您收到该消息的事实表明您正在使用 XSLT 2.0,它不允许将多个元素的序列作为参数提供给需要字符串参数的函数。

    如果您确实使用的是 XSLT 2.0(或更高版本),您可以这样做...

    <xsl:value-of select="descendant::contrib[@contrib-type='author']/concat(name/given-names, ' ', name/surname, ', ', degrees)" separator=" " />
    

    (注意,默认的分隔符无论如何都是空格,所以在这种情况下你可以省略分隔符属性,但我留下它只是为了演示它的使用)。

    另请注意,在 XSLT 1.0 中,xsl:value-of 只会返回节点集中第一个节点的值,因此您必须这样编写:

    <xsl:for-each select="descendant::contrib[@contrib-type='author']">
        <xsl:if test="position() > 1"><xsl:text> </xsl:text></xsl:if>
        <xsl:value-of select="concat(name/given-names, ' ', name/surname, ', ', degrees)" />
    </xsl:for-each>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多