Subject: [xsl] Default values of template parameters From: "Julian Cox" <julian.cox@xxxxxxxxxxxx> Date: Tue, 1 Aug 2006 11:57:32 +0100 |
Hi all, Question for you regarding the use of default values in called templates. Apologies for the length of this post. Below are code fragments loosely based on my real application, I doubt it is workable code but hopefully it will illustrate my question. I'm using msxsl.exe (xslt 1.0, xpath 2.0) and Altova XMLSpy for development. I have a couple of hundred source xml docs that might go something like this: <shapes> <rectangle left="200" top="0" right="500" bottom="700" /> <rectangle left="250" top="50" right="550" bottom="750" /> </shapes> I have written a transform that turns all the source xml files into a single comma separated variables file. There will be a single line in the csv for all shapes. After I'm gone I need the users of my application to be able to control and adjust how each shape is transformed but they know nothing of xslt so I created a mapping file called mapping.xml something like this: <shapemapping> <shape name="rectangle" linewidth="1" linecolour="Blue" /> <line name="diagonal" direction="up" linewidth="0.5" linecolour="LightBlue"/> <line name="diagonal" direction="down" linewidth="0.5" linecolour="LightBlue"/> <shape name="rhombus" linewidth="1" linecolour="Green" /> </shapemapping> For each shape this is a set of instructions on what to do. Whenever a rectangle is encountered the diagonal lines will be added. My transform then looks like this: <!-- Start of pattern match templates --> <xsl:template match="shapes" > <xsl:apply-templates> </xsl:template> <xsl:template match="rectangle | rhombus | square | circle | triangle" > <xsl:variable name="MatchedEntityName" select="name()" /> <xsl:variable name="MappingList" select="document(mapping.xml)/shapemapping" /> <!-- process the parent shape --> <xsl:call-template name="OutputShapeData" > <xsl:with-param name="height" select="@bottom-@top" /> <xsl:with-param name="width" select="@right-@left"/> <xsl:with-param name="top" select="@top"/> <xsl:with-param name="left" select="@left"/> <xsl:with-param name="shape" select="name()" /> <xsl:with-param name="lineweight" select="$MappingList/shape[@name = $MatchedEntityName]/@linewidth" /> <xsl:with-param name="linecolour" select="$MappingList/shape[@name = $MatchedEntityName]/@linecolour" /> </xsl:call-template> <!-- process any child parameters --> <xsl:for-each select="$MappingList/shape[@name = $MatchedEntityName]/line" <xsl:call-template name="OutputShapeData" > <xsl:with-param name="height" select="@bottom-@top" /> <xsl:with-param name="width" select="@right-@left"/> <xsl:with-param name="top" select="@top"/> <xsl:with-param name="left" select="@left"/> <xsl:with-param name="shape" select="line" /> <xsl:with-param name="lineweight" select="@linewidth" /> <xsl:with-param name="linecolour" select="@linecolour" /> </xsl:call-template> </xsl:template> <!-- match all remaining templates --> <xsl:template match="*"/> <!-- End of pattern match templates --> <!-- All templates from here are called --> <xsl:template name="OutputShapeData" > <!-- outputs one line to .csv --> <xsl:param name="height" /> <xsl:param name="width" /> <xsl:param name="top" /> <xsl:param name="left" /> <xsl:param name="shape" /> <xsl:param name="lineweight" /> <xsl:param name="linecolour" /> <xsl:variable name="UnusedParams" select="0,0,1,300,Solid,,40,0,0,0,0" <xsl:text>$height</xsl:text> <xsl:text>,</xsl:text> Etc for other parameters <xsl:text>$UnusedParams</xsl:text>&cr; </xsl:template> <!-- End of called templates --> And it works very well. But users being users instantly start asking for bells and whistles. They want to be able to optionally specify a line style, optionally because they don't want the mapping file becoming huge (read: can't be bothered to put it into all thousand or so lines of the real mapping file). So each shape and line in the mapping file optionally has linestyle="dotted", or dashed or centreline or whatever value. So my template can easily becomes: <xsl:template name="OutputShapeData" > <!-- ouputs one line to .csv --> <xsl:param name="height" /> <xsl:param name="width" /> <xsl:param name="top" /> <xsl:param name="left" /> <xsl:param name="shape" /> <xsl:param name="lineweight" /> <xsl:param name="linecolour" /> <xsl:param name="linestyle" select="Solid" /> <xsl:variable name="UnusedParamsA" select="0,0,1,300," /> <xsl:variable name="unusedParamsB" select=",,40,0,0,0,0" /> Etc And I added this to the function calls: <xsl:with-param name="linestyle" > <xsl:choose> <!-- if a linestyle is specified...--> <xsl:when test="[@name = $MatchedEntityName]/@linestyle"> <!-- ... pass it's value, otherwise... --> <xsl:value-of select="$MatchedEntityName]/@linestyle " /> </xsl:when> <xsl:otherwise> <!-- pass nothing. --> <xsl:value-of select="''"/> </xsl:otherwise> </xsl:choose> </xsl:with-param> But linestyle exists in either case so the default value for linestyle, 'Solid', is never taken. I can make both instances where the template is called like this: <xsl:otherwise> <xsl:value-of select="Solid"/> </xsl:otherwise> But that makes a nonsense of default values for optional parameters because it can never be used. It also means I've got 2 places to maintain a default value in the code. Not good. Is there a way to pass the parameter if the tested attribute exists and not pass anything, not even an empty string, if it does not? Cheers Julian Julian Cox BSI Development Engineer Ultra Electronics Airport Systems Web: www.ultra-as.com ________________________________________________________________________ "The information in this email and any attached files is confidential. It is intended solely for the use of the addressee. Any unauthorised disclosure or use is prohibited. If you are not the intended recipient of the message, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. The views of the author may not necessarily reflect those of the company. The contents of an attachment to this e-mail may contain software viruses which could damage your own computer system. While Ultra Electronics Limited has taken every reasonable precaution to minimise this risk, we cannot accept liability for any damage which you sustain as a result of software viruses. You should carry out your own virus checks before opening the attachment."
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Sibling axis and as="elem, Michael Kay | Thread | Re: [xsl] Default values of templat, David Carlisle |
Re: [xsl] Sibling axis and as="elem, David Carlisle | Date | Re: [xsl] Sibling axis and as="elem, David Carlisle |
Month |