Search code examples
xsltparamnode-set

Unable to cast from XRTreeFrag into XNodeSet


I have the following test code... I am trying to pass a node-set as a param. After many hours, i finally was able to pass it to my template.

How I pass my node-set to the template:

<xsl:call-template name="listing">
    <xsl:with-param name="customData">
        <xsl:apply-templates select="exslt:node-set($data)"/>
    </xsl:with-param>
</xsl:call-template>

How my template receives it:

<xsl:template name="listing">
    <xsl:param name="customData" select="/.."/>
    <xsl:variable name="data">
        <xsl:choose>
            <xsl:when test="not($customData)">
                <xsl:value-of select="/data"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$customData"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <textarea><xsl:copy-of select="$data"></xsl:copy-of></textarea>
</xsl:call-template>

If I set the parameters with a one liner, then it would not complain... example:

<xsl:variable name="data" select="$customData"/>

But as soon as I try to set it like this, it breaks:

<xsl:variable name="data">
   <xsl:value-of select="$customData"/>
</xsl:variable>

Getting this error message: org.apache.xpath.objects.XRTreeFrag cannot be cast to org.apache.xpath.objects.XNodeSet

I was only been able to find another thread dated back in 2000, talk about this similar issue... I need to re-nodeset it back using something like node-set($customData)/* but I tried that, and it was a no go.

EDIT: OK, I can confirm that I successfully passed the node-set inside my template. But I'm still unable to copy it over to my variable... It kept saying that it is still a RTF.

<xsl:template name="listing">
<xsl:param name="customData" as="node-set"/>
<!--<xsl:variable name="data" select="/data"/>-->

<xsl:variable name="data">
    <xsl:choose>
        <xsl:when test="count($customData) != 0">
            <xsl:copy-of select="$customData"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/data"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

    <textarea><xsl:value-of select="$customData/record[1]"/></textarea>
    <textarea><xsl:value-of select="/data/record[1]"/></textarea>
    <textarea><xsl:value-of select="$data/record[1]"/></textarea>
</xsl:template>

The above test, shows that I can access $customData and the original /data without any problem, they both show the record... but $data is messed up. So that means the copy from $customData to $data wasn't working...

I tried the following ways, none of them work:

<xsl:copy-of select="$customData"/>
<xsl:value-of select="$customData"/>
<xsl:apply-templates select="exslt:node-set($customData)"/>
<xsl:apply-templates select="exslt:node-set($customData)/data"/>

Any idea...?


Solution

  • Got it working, basically rather than using apply-template, i need to pass the RTF as a parameter to the template. That is the only way I got it to work.

    <xsl:with-param name="data" select="exslt:node-set($customData)"/>
    

    Using this method, I was able to MODIFY data in XSL level. This is really cool, I basically manipulate the data I want, then i reconstruct the root /, and then I pass my customData to my template function.

    So rather than reading data off the root, I read my own modified data (constructed inside XSL).