FM Variables with XSLT

Version

Date

Changes

1.1

2010-09-09

Note about Automap. Also see "Discussion" page for tips on getting this to work.

1.0

2009-06-12

Initial version.

Description

ePublisher does not provide direct access to Framemaker variables in code. This topic shows how to use XSLT to retrieve the value of a Framemaker variable for use in transformations.

How it works

When ePublisher does a "Scan All Documents" of a Framemaker book or document, it includes the Framemaker variables and values in the project.cache. The getfmvars.xslt transformation described below scans the cache and returns the value of a named variable. If it does not find the variables, it returns a default value passed in from the caller.

The project.cache file is in the data directory. Locate the file like this:

<xsl:variable name="ProjectCacheFile" select="concat(wwprojext:GetProjectDataDirectoryPath(),'\project.cache' )" />

Once you have the file, retrieve the value of the variable whose name was passed in as a parameter, like this:

<xsl:param name="ParamVarName" />
<xsl:variable name="VariableValue" 
              select="wwexsldoc:LoadXMLWithoutResolver($ProjectCacheFile)//proj_cache:Variable[@Name=$ParamVarName]/@Value" />

Calling getfmvars.xslt

This example calls the transformation by looking for a variable named BookName. If that variable is not in the cache, it uses Default Book Title instead:

<xsl:include href="wwformat:Transforms/getfmvars.xsl" />
...
<xsl:variable name="VarBookTitle">
 <xsl:call-template name="GetVarFromProjectCache">
   <xsl:with-param name="ParamVarName" select="'BookName'" />
   <xsl:with-param name="ParamDefaultValue" select="'Default Book Title'" />
 </xsl:call-template>
</xsl:variable>

Errors are logged when the project cache does not exist, and when the named variable is not in the cache.

Code listing for getfmvars.xslt

This is the entire code listing for getfmvars.xslt when used as part of a Dynamic HTML format.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="urn:WebWorks-Reports-Schema"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:proj_cache="http://tempuri.org/ProjectCache.xsd"
                              xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                              xmlns:context="urn:WebWorks-Reports-Schema"
                              xmlns:wwreport="urn:WebWorks-Reports-Schema"
                              xmlns:wwsplits="urn:WebWorks-Engine-Splits-Schema"
                              xmlns:wwlinks="urn:WebWorks-Engine-Links-Schema"
                              xmlns:wwlocale="urn:WebWorks-Locale-Schema"
                              xmlns:wwmode="urn:WebWorks-Engine-Mode"
                              xmlns:wwfiles="urn:WebWorks-Engine-Files-Schema"
                              xmlns:wwbehaviors="urn:WebWorks-Behaviors-Schema"
                              xmlns:wwdoc="urn:WebWorks-Document-Schema"
                              xmlns:wwproject="urn:WebWorks-Publish-Project"
                              xmlns:wwprogress="urn:WebWorks-XSLT-Extension-Progress"
                              xmlns:wwlog="urn:WebWorks-XSLT-Extension-Log"
                              xmlns:wwfilesystem="urn:WebWorks-XSLT-Extension-FileSystem"
                              xmlns:wwuri="urn:WebWorks-XSLT-Extension-URI"
                              xmlns:wwstring="urn:WebWorks-XSLT-Extension-StringUtilities"
                              xmlns:wwfilesext="urn:WebWorks-XSLT-Extension-Files"
                              xmlns:wwprojext="urn:WebWorks-XSLT-Extension-Project"
                              xmlns:wwexsldoc="urn:WebWorks-XSLT-Extension-Document"
                              xmlns:wwexec="urn:WebWorks-XSLT-Extension-Execute"
                              xmlns:wwenv="urn:WebWorks-XSLT-Extension-Environment"
                              exclude-result-prefixes="xsl proj_cache msxsl wwsplits wwlinks wwlocale wwmode wwfiles wwbehaviors wwdoc wwproject wwprogress wwlog wwfilesystem wwuri wwstring wwfilesext wwprojext wwexsldoc wwexec wwenv"
>

 <xsl:variable name="ProjectCacheFile" select="concat(wwprojext:GetProjectDataDirectoryPath(),'\project.cache' )" />

 <!-- Retrieves the value of a FrameMaker variable from the ePublisher data cache.
      Note that the cache does NOT get updated manually.  You might have to manually
      run a SCAN ALL DOCUMENTS from the GUI to build the cache.  I don't see how to 
      do this automatically.
 -->
 <xsl:template name="GetVarFromProjectCache">
  <xsl:param name="ParamVarName" />
  <xsl:param name="ParamDefaultValue" />
  
  <xsl:choose>
    <xsl:when test="wwfilesystem:Exists($ProjectCacheFile)">
      <xsl:variable name="VariableValue" 
                    select="wwexsldoc:LoadXMLWithoutResolver($ProjectCacheFile)//proj_cache:Variable[@Name=$ParamVarName]/@Value" />
      <xsl:variable name="VariableValueToUse">
        <xsl:choose>
          <xsl:when test="string-length($VariableValue) &gt; 0 ">
           <xsl:value-of select="$VariableValue" />
          </xsl:when>
          <xsl:otherwise>
           <xsl:value-of select="$ParamDefaultValue" />
           <xsl:value-of select="wwlog:Error(concat('Could not find [', $ParamVarName, '] variable from FM file in project.cache.  Using [', 
                                                     $ParamDefaultValue, '] instead.'))" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="$VariableValueToUse" />
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$ParamDefaultValue" />
     <xsl:variable name="LogNoProjectCacheMessage" 
                   select="wwlog:Error(concat('Project cache file [', $ProjectCacheFile, '] does not exist. Cannot retrieve value of [', 
                                               $ParamVarName, ']. Using [',$ParamDefaultValue,'] instead'))" />
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

</xsl:stylesheet>


CategorySolutionsInputFrameMaker CategorySolutionsOutputXmlAndXsl

DevCenter/Projects/FM Variables with XSLT (last edited 2010-09-13 18:30:36 by DaveTruman)