且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用xslt 2.0解析BBCode

更新时间:2023-02-22 22:08:41

我认为一种更可行的方法是反复匹配并替换(通过正则表达式)BBcode标签的,直到没有得到为止火柴.例如.对于[quote][url]:

I think a more viable approach would be to repeatedly match and replace (via regex) pairs of BBcode tags, until you get no matches. E.g. for [quote] and [url]:

<xsl:function name="my:bbcode-to-xhtml" as="node()*">
  <xsl:param name="bbcode" as="xs:string"/> 
  <xsl:analyze-string select="$bbcode" regex="(\[quote\](.*)\[/quote\])|(\[url=(.*?)\](.*)\[/url\])" flags="s">
    <xsl:matching-substring>
      <xsl:choose>
        <xsl:when test="regex-group(1)"> <!-- [quote] -->
          <span class="quote">
            <xsl:value-of select="my:bbcode-to-xhtml(regex-group(2))"/>
          </span>
        </xsl:when>
        <xsl:when test="regex-group(3)"> <!-- [url] -->
          <a href="regex-group(4)">
            <xsl:value-of select="my:bbcode-to-xhtml(regex-group(5))"/>
          </a>
        </xsl:when>
      </xsl:choose>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:function>