如何用逗号修改XSL拆分中的值

时间:2016-11-04 15:06:01

标签: xml xslt xslt-1.0

XSL:

<xsl:value-of select="theName" />

示例值(最后,第一个中间,标题):

- Fitzerald, John K., MBBS
- Keane, Mike

如何拆分theName,取上面的示例值,所以它显示如下:

- John K. Fitzerald MBBS
- Mike Keane

1 个答案:

答案 0 :(得分:1)

这是使用tokenize() ...

的XSLT 2.0选项

XML输入

<doc>
    <theName>Fitzerald, John K., MBBS</theName>
    <theName> Fitzerald , John  K. , MBBS  </theName>
    <theName>Keane, Mike</theName>
</doc>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="theName">
    <xsl:variable name="tokens" 
      select="for $token in tokenize(.,',') return normalize-space($token)"/>
    <xsl:copy>
      <xsl:value-of select="($tokens[2],$tokens[1],$tokens[3])" separator=" "/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

<强>输出

<doc>
   <theName>John K. Fitzerald MBBS</theName>
   <theName>John K. Fitzerald MBBS</theName>
   <theName>Mike Keane</theName>
</doc>