Home > front end >  xml-formatted text to e.g. markdown-text
xml-formatted text to e.g. markdown-text

Time:11-22

I'm quite new with xslt and I have a xml-formatted text to transform to a different text format e.g. markdown. xml-formatting nodes can occur several times inside a text content as in the following simplified example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<content>
    <para>
        This is a sample
      <bold>text</bold>
        for testing and
        <bold>improvement</bold>
        of current software version
    </para>
</content>

My xslt transformation (from JDK 1.8.0) is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="no" omit-xml-declaration="yes" xml:space="default" />

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="para">
        <xsl:for-each select="./*">
            <xsl:choose>
                <xsl:when test="bold">
                    <xsl:value-of select="normalize-space(bold/preceding-sibling::text())"/>
                    <xsl:text> **</xsl:text><xsl:value-of select="normalize-space(bold)"/><xsl:text>** </xsl:text>
                    <xsl:value-of select="normalize-space(bold/following-sibling::text())"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(.)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

which outputs just the string: textimprovement.

How can i get a better transformation ?

Thanks for helping.

CodePudding user response:

I would start with e.g.

<xsl:template match="bold">**<xsl:apply-templates/>**</xsl:template>

and write templates matching and transforming other elements as needed.

CodePudding user response:

Use template rules.

Something like

<xsl:template match="para">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="bold">
  <xsl:text>**</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>**</xsl:text>
</xsl:template>

The idea is to perform a recursive descent of the tree, processing each node that you reach using the best-fit template rule for that node. This is the classic XSLT processing model for mixed content.

  • Related