|
next
|
 Subject: Trouble trying to pass an XPath as a Template param Author: Ian Cargill Date: 24 Aug 2005 04:07 PM
|
I'm fairly new to XSLT and although I've been able to resolve most of my problems fairly quickly with a little web (and book!) browsing, this one has me stumped, so I'd really appreciate a pointer. (Also, excuse me if I don't have the jargon quite right yet! :-)
I have to pick off a lot of elements of an XML, apply a little logic, and write (or not) an output element. Ideal for parameterised templates, so I wrote some. In the simplest case, the template needs to get at two elements that depend on an XPath; a tag value (?) and an element.
Now if I 'decode' these values in the calling script and pass the actual values as parameters to the template, it works fine (example hopefully pasted in at the end!) But this means specifying the same XPath twice (and for some templates 4 times)), with consequent certainty of some not matching properly (oh, woe, cut-and-paste!).
It would be soooooo much nicer (and safer) to specify the XPath just once, as a param to the template, then let the template derive the valies for the elements (attribute, etc). But try as I might, I just can't work out the correct syntax. I'd really appreciate a push in the right direction.
Don't know if this will display properly, but here's a truncated excerpt of the simplest template and a call...
<pre>
<xsl:template name="WriteTagNotNull">
<xsl:param name="NewTag"/>
<xsl:param name="Source"/>
<xsl:param name="SourceContent"/>
<xsl:param name="Type"></xsl:param>
<!-- Only write the tag if there is data in the source element OR the element is tagged as 'empty' -->
<xsl:if test="(string-length($Source) > 0) or ($SourceContent = 'empty') ">
<xsl:element name="{$NewTag}">
<xsl:if test="string-length($Type) > 0">
<xsl:attribute name="type"><xsl:value-of select="$Type"/></xsl:attribute>
</xsl:if>
<xsl:value-of select="$Source"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template name="Data">
<xsl:call-template name="WriteTagNotNull">
<xsl:with-param name="NewTag" select="'GPCode'"/>
<xsl:with-param name="Source" select="/XDI/DATA/PD1/PatientPrimaryCareProviderNameAndIDNo/C1/S1"/>
<xsl:with-param name="SourceContent" select="/XDI/DATA/PD1/PatientPrimaryCareProviderNameAndIDNo/C1/S1/@content"/>
</xsl:call-template>
</xsl:template>
</pre>
|
|
|