开发者

Xalan extension element - can not select variable

开发者 https://www.devze.com 2023-02-12 08:33 出处:网络
I have written a simple Xalan extension which selects a node and returns it\'s text. The XPath processor works fine until I try to use variable reference. I.e.:

I have written a simple Xalan extension which selects a node and returns it's text. The XPath processor works fine until I try to use variable reference. I.e.:

<tst:extract select="node2" /> 

produces correct result (node content), but

<tst:extract select="$var1" />

generates an error: Variable not resolvable: var1. What do I need is to resolve both variables (and template params) and other XPath expressions.

There has been similar problem (XALANJ-1136) but concerning extension functions, not elements. Am I doing something wrong, or it is another bug?

The code is below:

package org.bogus.xalan;

import javax.xml.transform.TransformerException;

import org.apache.xalan.extensions.XSLProcessorContext;
import org.apache.xalan.templates.ElemExtensionCall;
import org.apache.xpath.XPath;
import org.apache.xpath.XPathContext;
import org.apache.xpath.objects.XObject;


public class Test
{
    public String extract(XSLProcessorContext ctx, 
        ElemExtensionCall extensionElement) 
    throws TransformerException
    {
        final String select = extensionElement.getAttribute("select");
        final XPathContext xctxt = ctx.getTransformer().getXPathContext();
        final XPath xpath = new XPath( select, extensionElement, 
            xctxt.getNamespaceContext(), XPath.SELECT);
        开发者_运维百科final XObject xobj = xpath.execute(xctxt, xctxt.getContextNode(), 
            xctxt.getNamespaceContext());
        if (xobj == null){
            return "NONE FOUND";
        }
        return (String)xobj.castToType(XObject.CLASS_STRING, xctxt);

    }
}

XSLT:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xalan="http://xml.apache.org/xalan"
                xmlns:tst="http://org.bogus/xalan/test"
                extension-element-prefixes="tst"
                >

<xalan:component prefix="tst" elements="extract" >
    <xalan:script lang="javaclass" src="xalan://org.bogus.xalan.Test"/>
</xalan:component>                

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:template match="data">
    <r>
        <value_of_node_1><xsl:value-of select="node1" /></value_of_node_1>
        <value_of_node_2><xsl:value-of select="node2" /></value_of_node_2>

        <xsl:variable name="var1" select="node1" />
        <xsl:variable name="var2">
            <xsl:value-of select="node2" />
        </xsl:variable>

        <xpath_of_var_1><tst:extract select="$var1" /></xpath_of_var_1>
        <xpath_of_var_2><tst:extract select="$var2" /></xpath_of_var_2>
        <xpath_of_node_2><tst:extract select="node2" /></xpath_of_node_2>

        <value_of_var_1><xsl:value-of select="$var1" /></value_of_var_1>
        <value_of_var_2><xsl:value-of select="$var2" /></value_of_var_2>
    </r>
</xsl:template>    
</xsl:stylesheet>    

And the generated result:

<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:xalan="http://xml.apache.org/xalan">
<value_of_node_1>Node 1 content</value_of_node_1>
<value_of_node_2>node-2-content</value_of_node_2>
<xpath_of_var_1>NONE FOUND</xpath_of_var_1>
<xpath_of_var_2>NONE FOUND</xpath_of_var_2>
<xpath_of_node_2>node-2-content</xpath_of_node_2>
<value_of_var_1>Node 1 content</value_of_var_1>
<value_of_var_2>node-2-content</value_of_var_2>
</r>

file:/E:/xslt/transform.xsl; Line #25; Column #55; Variable not resolvable: var1
file:/E:/xslt/transform.xsl; Line #26; Column #55; Variable not resolvable: var2

I'm using Xalan 2.7.1


This is a kind of regression bug that has been introduced in revision 338150 in org.apache.xpath.operations.Variable. To solve this, either switch to Xalan-2.6.0 or recompile xalan and use method execute(XPathContext, boolean) from older release.

0

精彩评论

暂无评论...
验证码 换一张
取 消