将XML的结果树片段视为字符串,并使用标记对其进行标记。作为分隔符

时间:2012-08-23 18:38:17

标签: xml xslt

如何转换存储在变量中的以下值。

<xsl:variable name="myvar">
There was a <b> super man </b> in the city. He was very brave.
</xsl:variable>

<p>There was a <b> super man </b> in the city.</p>
<p>He was very brave.</p>

使用XSLT 1.0模板?

1 个答案:

答案 0 :(得分:0)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext xsl">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vMarkup" select="/*/node()"/>

 <xsl:template match="/" name="split">
  <xsl:param name="pNodes" select="$vMarkup"/>

  <xsl:if test="$pNodes">
      <xsl:variable name="vTextWithDot" select="$pNodes[self::text()][contains(., '.')]"/>

      <xsl:choose>
        <xsl:when test="not($vTextWithDot)">
          <p><xsl:copy-of select="$pNodes"/></p>
        </xsl:when>
        <xsl:otherwise>
         <p>
          <xsl:copy-of select="$vTextWithDot/preceding-sibling::node()"/>
          <xsl:copy-of select=
          "concat(substring-before($vTextWithDot, '.'), '.')"/>
         </p>

         <xsl:if test="substring-after($vTextWithDot, '.')
                      or $vTextWithDot/following-sibling::node()">
             <xsl:variable name="vrtfNewNodes">
               <xsl:copy-of select="substring-after($vTextWithDot, '.')"/>
               <xsl:copy-of select="$vTextWithDot/following-sibling::node()"/>
             </xsl:variable>

             <xsl:call-template name="split">
               <xsl:with-param name="pNodes" select=
                  "ext:node-set($vrtfNewNodes)/node()"/>
             </xsl:call-template>
         </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

应用于此XML文档时

<t>There was a <b> super man </b> in the city. He was very brave.</t>

生成想要的正确结果

<p>There was a <b> super man </b> in the city.</p>
<p> He was very brave.</p>