XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Ian CargillSubject: 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) &gt; 0) or ($SourceContent = 'empty') ">
<xsl:element name="{$NewTag}">
<xsl:if test="string-length($Type) &gt; 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>

Posttop
Ivan PedruzziSubject: Trouble trying to pass an XPath as a Template param
Author: Ivan Pedruzzi
Date: 30 Aug 2005 02:04 AM

nothing should prevent you to do

<xsl:template name="WriteTagNotNull">
<xsl:param name="NewTag"/>
<xsl:param name="Source"/>
<xsl:param name="Type"/>

<xsl:if test="(string-length($Source) &gt; 0) or ($Source/@content = 'empty') ">
<xsl:element name="{$NewTag}">
<xsl:if test="string-length($Type) &gt; 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>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.