xsl-list
[Top] [All Lists]

[xsl] Can this hard-coded template be generalized?

2011-10-02 15:00:53
Below are two abbreviated but complete files.
They produce the HTML output I want, but only by hard coding my XSLT stylesheet (the code in question is in <xsl:template name="page">). The stylesheet writes to the working directory.

Note that I have hardcoded the numbers 105, 106, 107, and 108 from the data (the XML file is below the stylesheet. Either the data is too impoverished for me to accomplish my end, or I am too inexperienced at XPath to generalize the code.

Thanks,
Mark
-----------------------

The XSLT stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:xs="http://www.w3.org/2001/XMLSchema";
 xmlns:cps="http://www.cpslib.org";
 exclude-result-prefixes="xs cps" version="2.0">

 <xsl:template match="Set">
   <xsl:apply-templates>
<xsl:with-param name="page-name-stem" tunnel="yes" select="concat(@year, '-')"/>
   </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="Stamp">
   <!-- Write out the stamp page -->
   <xsl:variable name="base-number" select="CatNumbers/@pofis-number"/>
   <xsl:call-template name="page">
<xsl:with-param name="file-type" select="concat($base-number, '.htm')"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="page">
   <xsl:param name="file-type"/>
   <xsl:param name="page-name-stem" tunnel="yes"/>
   <xsl:result-document href="{concat($page-name-stem, $file-type)}">
     <html>
       <head/>
       <body>
         <xsl:for-each-group select="../Stamp/Formats" group-by="@complex">
<xsl:variable name="file-name" select="concat(@complex, 's.htm')"/>

<!-- Hardcode starts here -->

<xsl:if test="@complex eq '105' and (cps:extract-number($file-type) eq '105' or cps:extract-number($file-type) eq '106')">
             <a lang="en" class="button" href="{$file-name}">Se-tenant</a>
             <a lang="cz" class="button" href="{$file-name}">Spojky</a>
           </xsl:if>
<xsl:if test="@complex eq '107' and (cps:extract-number($file-type) eq '107' or cps:extract-number($file-type) eq '108')">
             <a lang="en" class="button" href="{$file-name}">Se-tenant</a>
             <a lang="cz" class="button" href="{$file-name}">Spojky</a>
           </xsl:if>
         </xsl:for-each-group>
       </body>
     </html>
   </xsl:result-document>
 </xsl:template>

 <xsl:function name="cps:extract-number">
   <xsl:param name="data"/>
   <xsl:value-of select="replace($data, '\P{N}', '')"/>
 </xsl:function>
</xsl:stylesheet>

------------------------------
The XML:

<?xml version="1.0" encoding="UTF-8"?>

<Set domain="cr" year="1996" month="3" day="27">
 <Stamp>
   <CatNumbers pofis-number="105"/>
   <Formats souvenir-sheet="105"/>
   <Formats complex="105"/>
 </Stamp>
 <Stamp>
   <CatNumbers pofis-number="106"/>
   <Formats souvenir-sheet="105"/>
   <Formats complex="105"/>
 </Stamp>
 <Stamp>
   <CatNumbers pofis-number="107"/>
   <Formats souvenir-sheet="105"/>
   <Formats complex="107" />
 </Stamp>
 <Stamp>
   <CatNumbers pofis-number="108"/>
  <Formats souvenir-sheet="105"/>
   <Formats complex="107" />
 </Stamp>
</Set>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--