SourceForge has been redesigned. Learn more.
Close

Java Extension Function returning Node

Help
Anonymous
2010-07-14
2012-10-08
  • Anonymous

    Anonymous - 2010-07-14

    I have a Java Extension function that returns a org.w3c.dom.Node and within
    the java function, I org.apache.xerces.dom.DocumentImpl() to generate a
    document containing several Nodes.

    When the Node is returned to XSLT, Saxon seems to be losing all XML tags, and
    only values are serialized to the output.

    I am using Saxon EE 9.2

     
  • Michael Kay

    Michael Kay - 2010-07-15

    You'll have to be more specific: show exactly what your extension function is
    doing, and exactly what the XSLT code is doing with the result. Then I'll be
    able to tell whether you are doing something wrong or whether there's a Saxon
    problem. Ideally provide enough information so that someone else can reproduce
    the problem.

     
  • Anonymous

    Anonymous - 2010-07-15

    Here's the Java Extension method that returns a Node.

        public Node getHeader()
        throws XSLTExtensionException
        {
            try
            {
                Document           doc = new org.apache.xerces.dom.DocumentImpl();
                Element            header = doc.createElement("Header");
    
                String fromID = "FromID";
                String toID   = "ToID";
    
                Node from = doc.createElement("From");
    
                Element identity = doc.createElement("Identity");
                Text t = doc.createTextNode( fromID );
                identity.appendChild(t);
                from.appendChild(identity);
    
                header.appendChild(from);
    
                Node to = doc.createElement("To");
    
                Element identity2 = doc.createElement("Identity");
                Text t2 = doc.createTextNode( toID );
                identity2.appendChild(t2);
                to.appendChild(identity2);
    
                header.appendChild(to);
    
                return header;
            }
            catch (Throwable t)
            {
                throw new XSLTExtensionException("getHeader " + t.getMessage());
            }
        }
    

    Here's a simple XSLT that invokes the extension and serializes the node.

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY DOCTYPE-SYSTEM "http://xml.test.org/schemas/test.dtd">
    ]>
    <xsl:stylesheet
            xmlns:xsl='[url]http://www.w3.org/1999/XSL/Transform[/url]'
            xmlns:saxon='[url]http://saxon.sf.net/[/url]'
            xmlns:util='java:com.test.xsltextension.Utilities'
            extension-element-prefixes='util'
            exclude-result-prefixes='util'
            version='1.0'>
    
        <xsl:output method='xml'
            version='1.0'
            encoding='UTF-8'
            omit-xml-declaration='no'
            doctype-system='&DOCTYPE-SYSTEM;'
            indent='yes' />
    
        <xsl:variable name="util" select="util:new()" />
    
        <xsl:template match="/">
            <xsl:call-template name='Envelope'/>
        </xsl:template>
    
        <xsl:template name='Envelope'>
            <TestXML>
                <xsl:call-template name="DocumentHeader"/> 
            </TestXML>
        </xsl:template>
    
        <xsl:template name="DocumentHeader">
            <xsl:value-of select="util:getHeader($util)"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    The output does not include XML tags for the node returned by the extension.

    <?xml version="1.0" encoding="UTF-8"?><TestXML xmlns:saxon="[url]http://saxon.sf.net/[/url]">[b]FromIDToID[/b]</TestXML>
    
     
  • Michael Kay

    Michael Kay - 2010-07-15

    The xsl:value-of instruction always produces a single text node. Try xsl:copy-
    of.

     

Log in to post a comment.