Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting simple markdown(string) to html with xslt

Tags:

People also ask

How XML is converted into HTML format using XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

What is XSLT in HTML?

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG.

Is XSL and XSLT the same?

XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


I'm transforming my XSLT-stylesheets into documentation, and I want a rich experience within the comment nodes for each code-chunk, therefore I want to convert the following comment and output as xhtml:

String:

# 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]

Wanted output:

<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>

I tried with a recursive function that uses xsl:analyze-string recursively from a ruleset, but can't find a solution that works really well.

Anyone have done this lately, or is there some frameworks out there that has functions to do this ?

thanx in advance! :)

Edit: Added one dirty example:

<!-- 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 &gt; 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>