Subject: Re: [xsl] Can this hard-coded template be generalized? From: "Mark" <mark@xxxxxxxxxxxx> Date: Sun, 2 Oct 2011 14:47:20 -0700 |
Thanks again, Mark
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.
How do you want it generalized? What, in a sentence, did you want to reflect abstractly that using the hard-coded numbers achieves.
For example, I was able to quickly reproduce your results with this much shorter 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="Stamp"> <xsl:result-document href="{../@year}-{CatNumbers/@pofis-number}.htm"> <html> <head/> <body> <a lang="en" class="button" href="{Formats/@complex}s.htm">Se-tenant</a> <a lang="cz" class="button" href="{Formats/@complex}s.htm">Spojky</a> </body> </html> </xsl:result-document> </xsl:template>
As before, it doesn't do any string decomposition after composition. Nor any un-grouping after grouping. And no tunneling was needed, nor even creating any variable and passing it as a subroutine as I was able to go and get the values as needed. I was looking at it declaratively and thinking about where to find tree nodes when needed.
But ... the code above seems too simple to reflect what you might have been trying with the grouping, so I have no idea if it will help you or not.
And I wasn't trying to be cryptic, just reducing creating variables where they weren't going to be reused often enough.
Note how in some (not all) instruction attributes (above in xsl:result-document/@href) and in all literal result element attributes (above in a/@href) you can mix multiple attribute value templates and boilerplate text strings to compose the attribute's value. You don't have to use concat() as you did below.
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>
-- Contact us for world-wide XML consulting and instructor-led training Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Can this hard-coded templ, G. Ken Holman | Thread | Re: [xsl] Can this hard-coded templ, G. Ken Holman |
Re: [xsl] Can this hard-coded templ, G. Ken Holman | Date | Re: [xsl] Can this hard-coded templ, G. Ken Holman |
Month |