Use XSLT to create a Flare Topic from XML Data

XSLT is language for transforming XML documents. The World Wide Web Consortium (W3C) published XSL Transformations (XSLT)

Version 1.0 as recommendation in 1999 and XSL Transformations (XSLT) Version 2.0 in 2007. The W3C recommendations are posted at:

http://www.w3.org/TR/xslt/

http://www.w3.org/TR/xslt20/

If you are lucky enough to have information housed as XML, you are just one transformation away from a Flare topic. XSLT can be used to create XHTML files from XML files. Suppose you have this XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <jellybeans>
    <jellybean>
      <flavor>coffee</flavor>
      <color>brown</color>
    </jellybean>
    <jellybean>
      <flavor>orange</flavor>
      <color>orange</color>
    </jellybean>
    <jellybean>
      <flavor>mint</flavor>
      <color>green</color>
    </jellybean>
  </jellybeans>
</root>

You can use XSLT to create this Flare topic:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
  <head />
  <body>
    <h1>Jellybeans</h1>
    <p>This is the Jellybeans table generated with an XML transformation.</p>
    <table style="width: 100%;border-collapse: separate;">
      <caption>Jellybeans</caption>
      <col style="width: 33%;" />
      <col style="width: 33%;" />
      <thead>
        <tr>
          <th>Flavor</th>
          <th>Color</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>coffee</td>
          <td>brown</td>
        </tr>
        <tr>
          <td>orange</td>
          <td>orange</td>
        </tr>
        <tr>
          <td>mint</td>
          <td>green</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

The XSLT used is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="root/jellybeans">
       <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
        <head>
        </head>
        <body>
          <h1>Jellybeans</h1>
          <p>This is the Jellybeans table generated with an XML transformation.</p>
          <table style="width: 100%;border-collapse: separate;">
            <caption>Jellybeans</caption>
            <col style="width: 33%;" />
            <col style="width: 33%;" />
            <thead>
              <tr>
                <th>Flavor</th>
                <th>Color</th>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each select="jellybean">
                <tr>
                  <td>
                    <xsl:value-of select="flavor"/>
                  </td>
                  <td>
                    <xsl:value-of select="color"/>
                  </td>
                </tr>
              </xsl:for-each>
            </tbody>
          </table>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

8 comments

  1. Nice. How do you actually “get” the generated xhtml output into a file that can be used in Flare? I saved your example XSLT into a file called flaretopic.xsl, and your example xml into another file called jellybeans.xml, and then added to the top of jellybeans.xml. When I open jellybeans.xml in a web browser, I can see the generated output, but if I try to view source, the browser just opens up the original xml file.

    1. You can use an XSLT processor to generate the topic. Two options are: Saxon XSLT and XQuery Processor and Command Line Transformation Utility (msxsl.exe). The second option is from Microsoft and it is for Windows. Then you would copy the generated file into the Flare project.

  2. It should say that I added < ?xml-stylesheet type=”text/xsl” href=”flaretopic.xsl”? > to the top of jellybeans.xml.

  3. Found some answers at http://stackoverflow.com/questions/1527300/viewing-source-in-xml-to-xhtml-transformation-through-xsl though I’m still curious as to which approach you’d take.

  4. Is it possible to use XSLT to create a whole Flare project? We currently have an automated process that takes XML through SAXON to create HTML and HTML Workshop project files which are then used to create a CHM file. It seems to me we should be able to write XSLT that can create a complete Flare project (including content files) as long as Flare doesn’t create any hidden project files we don’t know about. Or am I missing something?

    Thanks for the great resource by the way.

    1. You can probably accomplish that. I tend to create files and copy those into an existing project. But there is no reason you can’t create everything. Keep in mind that copies of projects are valid projects. If you have a template, you can make copies of that with Flare or outside of Flare. So some of the work can be done without XSLT. Thanks for reading!

Leave a Reply to Thomas Tregner Cancel reply

Your email address will not be published.

HTML tags are not allowed.

250,814 Spambots Blocked by Simple Comments