xsl variable $node/text() but $node can non-node-set help!

Discussion in 'XML' started by Tjerk Wolterink, Aug 23, 2006.

  1. I have a variable $value as a parameter in the following template:

    <xsl:template name="myTemplate">
    <xsl:param name="value"/>
    <xsl:if test="$value">
    <xsl:value-of select="$value"/>
    </xsl:if>
    </xsl:template>

    Now i call myTemplate sometimes whit the a parameter $value that
    is sometimes a text node, and sometimes it is not even a node.
    Example:

    <inputDoc>
    <a>hello</a>
    <a/>
    </inputDoc>

    xsl:

    <xsl:for-each select="/inputDoc/a">
    <xsl:call-template name="myTemplate">
    <xsl:with-param name="value" select="./text()"/>
    </xsl:call-template>
    </xsl:for-each>


    So i call myTemplate with parameter $value=a/text()

    But for the second <a> element in <inputDoc> there
    is not text() node.

    This gives me the following error:

    Cause: javax.xml.transform.TransformerException: The value is not a
    node-set

    With line number of the error pointing to the xsl:if in myTemplate.


    No i tried the function nilled(), but that is XPath 2.0, i only
    use XPath 1.0 and XSL 1.0

    i know a possible solution is this:

    <xsl:for-each select="/inputDoc/a">
    <xsl:choose>
    <xsl:when test=".[not(node())]">

    <xsl:call-template name="myTemplate">
    <xsl:with-param name="value" select="false()"/>
    </xsl:call-template>

    </xsl:when>
    <xsl:eek:therwise>

    <xsl:call-template name="myTemplate">
    <xsl:with-param name="value" select="./text()"/>
    </xsl:call-template>

    </xsl:eek:therwise>
    </xsl:for-each>


    But this requires me to change every call to myTemplate,
    which is alot of code. Can't i change myTemplate so
    it works?

    Who can help me? Please..
     
    Tjerk Wolterink, Aug 23, 2006
    #1
    1. Advertising

  2. Tjerk Wolterink wrote:

    > I have a variable $value as a parameter in the following template:
    >
    > <xsl:template name="myTemplate">
    > <xsl:param name="value"/>
    > <xsl:if test="$value">
    > <xsl:value-of select="$value"/>
    > </xsl:if>
    > </xsl:template>
    >
    > Now i call myTemplate sometimes whit the a parameter $value that
    > is sometimes a text node, and sometimes it is not even a node.
    > Example:
    >
    > <inputDoc>
    > <a>hello</a>
    > <a/>
    > </inputDoc>
    >
    > xsl:
    >
    > <xsl:for-each select="/inputDoc/a">
    > <xsl:call-template name="myTemplate">
    > <xsl:with-param name="value" select="./text()"/>


    That means the value you pass in _is_ a node set (of text nodes) but
    that node set might be empty. I don't see why that should give you an
    error for <xsl:if test="$value">, doing that test on an empty node set
    should simply yield false.

    > This gives me the following error:
    >
    > Cause: javax.xml.transform.TransformerException: The value is not a
    > node-set
    >
    > With line number of the error pointing to the xsl:if in myTemplate.


    What XSLT processor is that? Are you sure the code is as simple as you
    have posted? Have you tried with different XSLT processors?


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 23, 2006
    #2
    1. Advertising

  3. There is nothing wrong with the posted code -- either the reason is in code
    that was not shown or the XSLT processor used is non-compliant.

    A typical advise/requirement is to post a complete (but the minimal
    possible) example of the xslt stylesheet and the source xml document, to say
    what is the expected result, what is the actual result and how the actual
    result differs from what was expected.

    Cheers,
    Dimitre Novatchev.


    "Tjerk Wolterink" <> wrote in message
    news:echh0i$9hm$...
    >I have a variable $value as a parameter in the following template:
    >
    > <xsl:template name="myTemplate">
    > <xsl:param name="value"/>
    > <xsl:if test="$value">
    > <xsl:value-of select="$value"/>
    > </xsl:if>
    > </xsl:template>
    >
    > Now i call myTemplate sometimes whit the a parameter $value that
    > is sometimes a text node, and sometimes it is not even a node.
    > Example:
    >
    > <inputDoc>
    > <a>hello</a>
    > <a/>
    > </inputDoc>
    >
    > xsl:
    >
    > <xsl:for-each select="/inputDoc/a">
    > <xsl:call-template name="myTemplate">
    > <xsl:with-param name="value" select="./text()"/>
    > </xsl:call-template>
    > </xsl:for-each>
    >
    >
    > So i call myTemplate with parameter $value=a/text()
    >
    > But for the second <a> element in <inputDoc> there
    > is not text() node.
    >
    > This gives me the following error:
    >
    > Cause: javax.xml.transform.TransformerException: The value is not a
    > node-set
    >
    > With line number of the error pointing to the xsl:if in myTemplate.
    >
    >
    > No i tried the function nilled(), but that is XPath 2.0, i only
    > use XPath 1.0 and XSL 1.0
    >
    > i know a possible solution is this:
    >
    > <xsl:for-each select="/inputDoc/a">
    > <xsl:choose>
    > <xsl:when test=".[not(node())]">
    >
    > <xsl:call-template name="myTemplate">
    > <xsl:with-param name="value" select="false()"/>
    > </xsl:call-template>
    >
    > </xsl:when>
    > <xsl:eek:therwise>
    >
    > <xsl:call-template name="myTemplate">
    > <xsl:with-param name="value" select="./text()"/>
    > </xsl:call-template>
    >
    > </xsl:eek:therwise>
    > </xsl:for-each>
    >
    >
    > But this requires me to change every call to myTemplate,
    > which is alot of code. Can't i change myTemplate so
    > it works?
    >
    > Who can help me? Please..
     
    Dimitre Novatchev, Aug 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,648
  2. CB
    Replies:
    1
    Views:
    613
    Johannes Koch
    Oct 2, 2003
  3. Karsten Weinert
    Replies:
    3
    Views:
    1,062
    Karsten Weinert
    Jul 5, 2004
  4. Replies:
    1
    Views:
    3,687
    A. Bolmarcich
    May 27, 2005
  5. Greg
    Replies:
    1
    Views:
    735
    Martin Honnen
    Jun 23, 2006
Loading...

Share This Page