Hi,

I don't think the variables will have been replaced by their values when 
the extension is called.  Instead you could create a result tree fragment 
and call an extension function passing in the result tree fragment such as 
in the following example.

Stylesheet:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xalan="http://xml.apache.org/xalan";
                xmlns:myclass="MyClass"
                extension-element-prefixes="myclass"
                version="1.0">


  <xalan:component prefix="myclass" functions="process">
    <xalan:script lang="javaclass" src="MyClass"/>
  </xalan:component>
 
  <xsl:variable name="var1" select="'This is variable 1.'"/>
  <xsl:variable name="var2" select="'This is variable 2.'"/>
  <xsl:variable name="rtf">
    <elem1 number="1">
      <xsl:value-of select="$var1"/>
    </elem1>
    <elem2 number="2">
      <xsl:value-of select="$var2"/>
    </elem2>
  </xsl:variable>

  <xsl:template match="/">
    <out>
      <xsl:value-of select="myclass:process($rtf)"/>
    </out>
  </xsl:template>
 
</xsl:stylesheet>

MyClass.java:

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MyClass {

    public void process(NodeList rtf) {
        for (int i = 0; i < rtf.getLength(); i++) {
            processNode(rtf.item(i), "");
        }
    }
 
    private void processNode(Node node, String indent) {
        System.out.println(indent + "Name: " + node.getNodeName() + ", 
Value: " + node.getNodeValue());
        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node attr = map.item(i);
            System.out.println(indent + "    Attr: " + attr.getNodeName() 
+ ", Value: " + attr.getNodeValue());
        }
        NodeList nodes = node.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            processNode(nodes.item(i), indent + "    ");
        }
    }
 
}

The output from the processNode method is:

Name: #document, Value: null
    Name: elem1, Value: null
        Attr: number, Value: 1
        Name: #text, Value: This is variable 1.
    Name: elem2, Value: null
        Attr: number, Value: 2
        Name: #text, Value: This is variable 2.


Erin Harris





Cynepnaxa <[EMAIL PROTECTED]> 
06/09/2007 10:12 PM

To
xalan-j-users@xml.apache.org
cc

Subject
Extension element with subelements







Hi, everybody!
I start to write simple extension element, but a problem occurs. I want to
access extension element's subelements in java+xalan. My xsl contains
following block of code:
<my-ext:request>
<body><xsl:value-of select="$body"/></body>
<contentType>text/html</contentType>
<metaData><![CDATA[<?xml version="1.0" encoding="utf-8"?>]]><metaData>
<xsl:value-of select="$metaData"/>
</metaData>
</metaData>
</my-ext:request>
In extension element Java-method i have "XSLProcessorContext context" and
"ElemExtensionCall elem". I can get contentType by calling for example
"elem.getFirstChild().getNextSibling().getFirstChild().getTextContent()",
but i want to access subelements by tag name(for example, by
elem.getElementsByTagName("metadata"), or XPath). I have no idea how to
access in java content of $body variable and xml - encapsulated $metaData.
All google - examples shining no light. Is simple xslt - subelements 
access
method exist? Big thanx for help.
P.S. Sorry for my English. I hope you understand me.

-- 
View this message in context: 
http://www.nabble.com/Extension-element-with-subelements-tf4395665.html#a12534751

Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Reply via email to