Subject: no way to avoid errors if getting both strings and elements?
From: "Mark D. Anderson" <mda@xxxxxxxxxxxxxx>
Date: Sat, 5 Feb 2000 21:46:42 -0800
|
i'm often frustrated by the lack in xslt of a node-type function that
would just tell me 'text' or 'element' or whatever (like the ref() function in perl).
if i'm in a template and handed something that could be
either, i'm screwed. if it is text, then i get xt's dreaded
"cannot convert to node-set" error when i do things like name($thing) or even $thing/*.
if is an element, and i simply do something like <xsl:value-of select="$thing">
or string($thing), then i run together its multiple text children, which i
don't want to do.
here is a stylesheet that shows the problem.
i'd love to see a version of the "analyze_node" function that will classify
anything handed to it....
thanks.
-mda
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="str" select="'a string'"/>
<xsl:variable name="el">
<root>a text child</root>
</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="analyze_node"><xsl:with-param name="thing" select="$str"/></xsl:call-template>
<xsl:call-template name="analyze_node"><xsl:with-param name="thing" select="$el"/></xsl:call-template>
</xsl:template>
<xsl:template name="analyze_node">
<xsl:param name="thing"/>
<xsl:message>analyzing node...</xsl:message>
<xsl:choose>
<xsl:when test="$thing/self::text()">
<xsl:message>this is a string: <xsl:value-of select="$thing"/></xsl:message>
</xsl:when>
<xsl:when test="$thing/child::text()">
<xsl:message>this is a node with a text child: <xsl:value-of select="$thing/text()"/></xsl:message>
</xsl:when>
<xsl:otherwise>i dunno</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
----sample output----
e:/xt-19991105/xt test4.xsl test4.xsl test4.html
file:/d:/mda/projects/xmlhacks/xmlidl/test4.xsl:1: analyzing node...
cannot convert to node-set
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|