我正在将我的XSLT样式表转换为文档,并且我希望为每个代码块在注释节点中提供丰富的体验,因此我希望将以下注释和输出如XHTML:使用xslt将简单降价(字符串)转换为html
字符串:
# This is a title with __bold__ text and *italic* # This is just a normal line - list point with some __bold__ - list point with a "link"[http://www.stackoverflow.com]
通缉输出:
<h1> This is a title with <strong>bold</strong> and <span>italic</span> </h1>
<p>This is a normal line</p>
<ul>
<li>list point with some <strong>bold</strong></li>
<li>list point with a <a href="http://www.stackoverflow.com">link</a></li>
</ul>
我试图与使用XSL递归函数:从规则分析串递归设置,但无法找到一个很好的解决方案。
任何人最近都这样做了,还是有一些框架有功能可以做到这一点?
thanx提前! :)
编辑:增加了一个肮脏的例子:
<!-- Output comments -->
<xsl:template match="comment()" mode="COMMENT">
<xsl:copy-of select="ips:groupReplace(normalize-space(.),
'
(.*)(\n|\r)(.*),
(.*)\*(.*)\*(.*),
(.*)\*\*(.*)\*\*(.*),
(.*)__(.*)__(.*),
(.*)#(.*)#(.*),
(.*)-(.*)
',
'
br,
span.italic,
span.bold,
strong,
h1,
li
')" />
</xsl:template>
<!-- Initializing the iterateRegex function -->
<xsl:function name="ips:groupReplace">
<xsl:param name="string" as="xs:string" />
<xsl:param name="search" />
<xsl:param name="replace" />
<xsl:variable name="regex" select="tokenize($search, ',')" />
<xsl:variable name="replacements" select="tokenize($replace, ',')" />
<xsl:copy-of select="ips:iterateRegex(count($replacements), $string, $regex, $replacements)" />
</xsl:function>
<!-- Iterate each regex -->
<xsl:function name="ips:iterateRegex">
<xsl:param name="counter" />
<xsl:param name="string" />
<xsl:param name="list_regex" />
<xsl:param name="list_replace" />
<xsl:variable name="newStr">
<xsl:analyze-string select="$string" regex="{normalize-space($list_regex[$counter])}" flags="xm">
<xsl:matching-substring>
<xsl:variable name="cc" select="contains($list_replace[$counter], '.')" />
<xsl:variable name="tag" select="normalize-space(if ($cc) then (substring-before($list_replace[$counter], '.')) else ($list_replace[$counter]))" />
<xsl:copy-of select="regex-group(1)" />
<xsl:choose>
<xsl:when test="normalize-space(regex-group(2)) = ''">
<xsl:element name="{$tag}" />
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$tag}" >
<xsl:if test="$cc">
<xsl:attribute name="class" select="substring-after($list_replace[$counter],'.')" />
</xsl:if>
<xsl:copy-of select="regex-group(2)" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="regex-group(3)" />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="count" select="number($counter) - 1" />
<xsl:choose>
<xsl:when test="$count > 0">
<xsl:copy-of select="ips:iterateRegex($count, $newStr, $list_regex, $list_replace)" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$newStr" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
为什么要尝试使用XSLT转换非XML内容?这完全是这份工作的错误工具。 – 2010-08-23 16:58:49
@Greg:这种类型取决于您可以使用的XSLT版本。对于XSLT 1.0来说,确实非常困难。但借助XSLT 2.0及其广泛的正则表达式和其他字符串处理支持以及任意的文本文档加载功能,它变得几乎微不足道。 – Abel 2010-08-23 17:00:45
@Sveisvei:您使用的是XSLT的哪个版本或处理器?你可以使用扩展方法或函数,如果是,使用什么语言? – Abel 2010-08-23 17:03:40