[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

no way to avoid errors if getting both strings and elements?

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
how to avoid errors
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.