This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: RE: Convert string to a list of nodes
- From: Jeff Beadle <Jbeadle at manh dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Tue, 4 Dec 2001 10:56:43 -0500
- Subject: RE: RE: [xsl] Convert string to a list of nodes
- Reply-to: xsl-list at lists dot mulberrytech dot com
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
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