2

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

<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 模板?

4

1 回答 1

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>
于 2012-08-24T13:26:10.390 回答