Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] Default values of template parameters

From: "Julian Cox" <julian.cox@------------>
To:
Date: 8/1/2006 11:02:00 AM
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."


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent