如何拆分单词并在xsl中放置标签?

时间:2014-12-16 13:10:19

标签: xslt

我有以下过程的HTML。

<p class="Keywords"><i>Keywords</i>: key1; key2; key3; key4; key5</p>

在上面一行中,关键字由逗号(,)分号(;)分隔。

输出应如下所示。

<keywords>
  <key>key1</key>
  <key>key2</key>
  <key>key3</key>
  <key>key4</key>
  <key>key5</key>
</keywords>

XSL:

<xsl:template match="p[@class='Keywords']">
  <keywords>
    <xsl:variable name="keyName" select="substring-after(., ': ')" />
    <xsl:for-each select="$keyName">
      <xsl:if test="contains($keyName, '; ')">
        <key><xsl:value-of select="." /></key>
      </xsl:if>
    </xsl:for-each>
  <keywords>
<xsl:template>

感谢您提前。

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助

  <xsl:template match="/">
    <keywords>
      <xsl:call-template name="oldData">
        <xsl:with-param name="oData" select="substring-after(p/text()[2],':')"/>
      </xsl:call-template>
    </keywords>
  </xsl:template>
  <xsl:template name="oldData">
    <xsl:param name="oData"/>
    <xsl:variable name="ofirst" select="substring-before($oData,';')"/>
    <xsl:variable name="olast" select="substring-after($oData,';')"/>
    <xsl:choose>
      <xsl:when test="($ofirst!='')">
        <key>
          <xsl:value-of select="$ofirst"/>
        </key>
        <xsl:call-template name="oldData">
          <xsl:with-param name="oData" select="substring-after($oData,concat($ofirst,';'))"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$olast!=''">
        <xsl:call-template name="oldData">
          <xsl:with-param name="oData" select="$olast"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <key>
          <xsl:value-of select="$oData"/>
        </key>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

答案 1 :(得分:0)

试试这个:XSLT v2.0

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="p[@class='Keywords']">
      <keywords>
        <xsl:variable name="keyName" select="substring-after(., ': ')" />
            <xsl:for-each select="tokenize($keyName, '; ')">
              <key><xsl:value-of select="." /></key>
            </xsl:for-each>
      </keywords>
</xsl:template>
</xsl:stylesheet>
相关问题