Subject: [xsl] How to call a template with a param From: Manpreet Singh <singhm@xxxxxxxxxxx> Date: Thu, 11 Nov 2004 17:14:21 +0530 |
Hi, I have the following source xml. <sRoot> <srec1> <sleaf1>123</sleaf1> </srec1> </sRoot> Following is my target structure <tRoot> <trec1> <tleaf1>456</tleaf1> </trec1> </tRoot> I have written the following xsl to convert the source structure to target structure. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <tRoot> <xsl:call-template name="exception"> <xsl:with-param name="xpath" select="sRoot/srec1"/> <xsl:with-param name="str-xpath" select="'sRoot-srec1'"/> </xsl:call-template> </tRoot> </xsl:template> <xsl:template name="exception"> <xsl:param name="xpath"/> <xsl:param name="str-xpath"/> <xsl:choose> <xsl:when test="count($xpath)=0"> <!-- This call-template statement should be executed in case if my source has an incomplete structure (or to say in other words, the xpath does not exist in my source xml) and the template following this one should get executed--> <xsl:call-template name="$str-xpath"> <!--PROCESSING STOPS HERE--> <xsl:with-param name="xpath" select="''"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="$xpath"> <xsl:with-param name="xpath" select="$str-xpath"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="sRoot/srec1" name="sRoot-srec1"> <xsl:param name="xpath"/> <trec1> <xsl:choose> <xsl:when test="$xpath!=''"> <xsl:for-each select="sleaf1"> <tleaf1> <xsl:value-of select="."/> </tleaf1> </xsl:for-each> </xsl:when> <!-- In case the call to this template comes from call-template the following xsl:otherwise clause should get executed--> <xsl:otherwise> <tleaf1/> </xsl:otherwise> </xsl:choose> </trec1> </xsl:template> </xsl:stylesheet> The code in the above xsl should work even if a particular node is not found in the source xml. For example, if i use this xsl to parse the following xml i should get the target structure correctly even if the nodes are missing in the source. Specimen: <sRoot/> My processing of xsl stops abruptly at the above mentioned point in the xsl. Any ideas why? If yes then is there a way out. Regards Manpreet Singh
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] XSL and uPortal, Aaron Johnson | Thread | RE: [xsl] How to call a template wi, Arun Sinha |
Re: [xsl] xsl:for-each vs. xsl:appl, Dimtre Novatchev | Date | RE: [xsl] How to call a template wi, Arun Sinha |
Month |