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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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...

<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:value-of select="$Source"/>

<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"/>

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:value-of select="$Source"/>

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!  

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.