Re: [xsl] Convert string to a list of nodes

Subject: Re: [xsl] Convert string to a list of nodes
From: Alexandra Duda <oladuda@xxxxxxxxx>
Date: 04 Dec 2001 17:22:26 +0100
Hello Jeff

Thanks for your time ;-)

My problem is actually a bit different. I want to pass to some template as a parameter string, containing names of some nodes from my XML document. This template, with name="maintemplate", should generate a HTML table. Each row of this table should be created from one of nodes listed in parameter string.
I have another template, name="rowtemplate" that creates a row with specified number of cells and fill the cells with some values. So, in big picture, I have to call some template for each element passed in parameter string. Why am I doing so? Because I'm trying to create a HTML report from my application, that is based on two XML files: dataModel and textDB. DataModel contains nodes, that are unique within document scope. Here also are contained values for nodes. TextDb contains some labels that should be presented to user - for example "Room width" instead of "w1" ;-)
The goal I'm trying to reach is : create a general template for generating tables in my reporting system and call this template several times, each time for different set of nodes.
Your code helped me in other part of generating this report. 
Now I only want to know the way to get a node which name I have listed in string parameter passed to a template. This is the main problem for me now.

Best regards
Alexandra

> here are two approaches to what I think your asking for:
> 
> approach 1:  (supplying the values pre-loaded into the att list
> constructor
> string)
> 
> the xml:
> <xml>
> <width>20</width>
> <class>label</class>
> </xml>
> 
> the xsl: (updates only)
> 	...
> 	<xsl:template match="/"><xsl:apply-templates /></xsl:template>
> 
> 	<xsl:template match="xml">
> 		<xsl:call-template name="build-attribute-list">
> 			<xsl:with-param name="src-attribute-list">
> 			(width,<xsl:apply-templates select="//width"/>)
> 			(class,<xsl:apply-templates select="//class"/>)
> 			</xsl:with-param>
> 		</xsl:call-template>
> 	</xsl:template>
> 
> 	<xsl:template match="width">
> 		<xsl:value-of select="text()"/>
> 	</xsl:template>
> 	
> 	<xsl:template match="class">
> 		<xsl:value-of select="text()"/>
> 	</xsl:template>
> 	...
> 
> the output:
> <attribute-list>
>    <attribute name="width" value="20" />
>    <attribute name="class" value="label" />
> </attribute-list>
> 
> 
> 
> approach 2:  (letting the constructor resolve the value based on the
> attribute name via a template match)
> 
> 
> the xsl: (updates only)
> 	...
> 	<xsl:template match="/"><xsl:apply-templates /></xsl:template>
> 	<xsl:template match="xml">
> 		<xsl:call-template name="build-attribute-list">
> 			<xsl:with-param name="src-attribute-list">
> 			(width,"")
> 			(class,"")
> 			</xsl:with-param>
> 		</xsl:call-template>
> 	</xsl:template>
> 
> 	<xsl:template match="width">
> 		<xsl:value-of select="text()"/>
> 	</xsl:template>
> 	
> 	<xsl:template match="class">
> 		<xsl:value-of select="text()"/>
> 	</xsl:template>
> 	...
>       	<xsl:template name="__att-list-constructor">
>             	<!-- recursive worker template for template
&gt; build-att-list-constructor -->
> 
>             	<!-- attribute-list = '(n,v)(n,v)(n,v) ... ' -->
>             	<xsl:param name="attribute-list" />
> 
>            		 <!-- grabs the first n-v pair -->
>            		 <xsl:variable name="pair" select="substring-before(
> substring-after( $attribute-list,'(' ), ')' )"/>
>             	<xsl:if test="contains($pair,',') and $pair != '' ">
>                	 	<xsl:element name="attribute" >
>                 	 		<xsl:variable name="the-name"
> select="normalize-space( substring-before($pair,',') )"/>
>                      			<xsl:attribute
> name="name"><xsl:value-of select="$the-name"/></xsl:attribute>
>                     	 		<xsl:variable name="the-value">
>                      				<xsl:apply-templates
> select="//*[local-name()=$the-name]"/>
>                      			</xsl:variable> 
>                      			<xsl:attribute name="value"
> ><xsl:value-of select="normalize-space($the-value)"/></xsl:attribute>
>                 		</xsl:element>
>                 		<xsl:call-template
> name="__att-list-constructor">
>                    		 	<xsl:with-param
> name="attribute-list" select="substring-after( $attribute-list, concat(
> substring-after( $pair, ',' ), ')' ) )"/>
>                		 </xsl:call-template >
>             	</xsl:if>
>       	</xsl:template>
> 	...
> 
> the output:
> <attribute-list>
>    <attribute name="width" value="20" />
>    <attribute name="class" value="label" />
> </attribute-list>
> 
> 
> If your going to do something like the second approach, then I'd
> re-engineer
> the "__att-list-constructor" template to only take in your delimeted name
> list ... which I guess you already did(?).
> 
> -Jeff
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread