You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Andrew Chamberlain <an...@comsine.co.uk> on 2008/07/01 17:02:53 UTC

Passing a node-set from XSLT to Java

Hi All,

I'm trying to pass a tree fragment from XSLT to Java in the following 
way, and I'm not sure if the node-set is getting through correctly.  The 
XML is:

  <gml:Polygon>
   ...
  </gml:Polygon>

and the XSLT is:

  <xsl:stylesheet ... myClass="xalan://my.package.name.MyClass">
    ...
    <xsl:variable name="gmlAdder" select="myClass:new()"/>

    <xsl:variable name="gml">
      <xsl:copy-of select="@* | node()"/>
    </xsl:variable>

    <xsl:variable name="result" select="myClass:addGML($gmlAdder,$gml)"/>
    ...
  </xsl:stylesheet>

where the addGML method is:

  public String addGML(org.w3c.dom.Node myNode)
  {
     ...
     return (myNode.getNodeName());
  }


This gives the 'result' variable the value of "#text", but when I was 
expecting "gml:Polygon".

Has anyone else managed to transfer a node-set to Java?  If so, did you 
use xalan?

Any tips would be greatly appreciated.

Thanks,

Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Passing a node-set from XSLT to Java

Posted by Luca Morandini <lm...@ieee.org>.
Andrew Chamberlain wrote:
> 
> The last line (which uses the getTextContent() method) throws the 
> following exception:
> 
> javax.xml.transform.TransformerException: java.lang.AbstractMethodError: 
> org.apache.xml.dtm.ref.DTMNodeProxy.getTextContent()Ljava/lang/String;
>        at 
> org.apache.xalan.extensions.ExtensionHandlerJavaClass.callFunction(ExtensionHandlerJavaClass.java:396) 

For a better understanding, you should look at the portion of the stack 
trace following "caused by".


> Firstly, I was expecting the Class to be "org.w3c.dom.Node", so does 
> anyone know how I can enable this?  

Rather difficult, since org.w3c.dom.Node is an interface, not a class: 
the object that is passed to the addGML function is an instance of 
org.apache.xml.dtm.ref.DTMNodeProxy, an actual class which happens to 
implement org.w3c.dom.Node.


> I was also expecting the name of the 
> first child to be "gml:exterior" instead of a "#text". 

Understandable, since your XML has newlines and blanks before 
gml:exterior: try stripping all this baggage before parsing.

Regards,

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Passing a node-set from XSLT to Java

Posted by Derek Hohls <DH...@csir.co.za>.
Andy

I think you stumbled across one of the "gotchas" of XML; what looks simple
and obvious is not always - at least, not to a machine.  For more in-depth 
understanding of XML, I can recommend Ken Sall to explain it.  There is a
sample chapter from his book on-line:
http://wdvl.internet.com/Authoring/Languages/XML/XMLFamily/XMLSyntax/
For example; see:
http://wdvl.internet.com/Authoring/Languages/XML/XMLFamily/XMLSyntax/sall2_4.html
where he explicitly says:
"In contrast to HTML, in which a sequence of white space characters is collapsed 
into a single white space and in which newlines are ignored, in XML 
all white space is taken literally. "

HTH
Derek


>>> On 2008/07/04 at 12:48, in message <48...@comsine.co.uk>, Andrew Chamberlain <an...@comsine.co.uk> wrote:
Hi Luca, Jascon,

Thanks very much - you've set me straight.  I hadn't imagined that the blank space would constitute a seperate node.

Andy



-- 
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.  MailScanner thanks Transtec Computers for their support.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Passing a node-set from XSLT to Java

Posted by Jason Johnston <jj...@apache.org>.
Andrew Chamberlain wrote:
> Hi Luca/All,
> 
> Sorry.  On further testing, we're still not getting through the full 
> tree fragment to Java.
> 
> I've adapted the java method to give details of what it receives:
> 
>  public String addGML(org.w3c.dom.Node myNode)
>  {
>       System.out.println("Class = "+node.getClass().getName());
>       System.out.println("Name = "+node.getNodeName());
>       System.out.println("Has children = 
> "+(node.hasChildNodes()?"Yes":"No"));
>       System.out.println("First Child Name = 
> "+node.getFirstChild().getNodeName());
>       System.out.println("First Child Text = 
> "+node.getFirstChild().getTextContent());
>  }
> 
> and from this comes the following output:
> 
>  Class = org.apache.xml.dtm.ref.DTMNodeProxy
>  Name = gml:Polygon
>  Has children = Yes
>  First Child Name = #text
> 
> The last line (which uses the getTextContent() method) throws the 
> following exception:
> 
> javax.xml.transform.TransformerException: java.lang.AbstractMethodError: 
> org.apache.xml.dtm.ref.DTMNodeProxy.getTextContent()Ljava/lang/String;
>        at 
> org.apache.xalan.extensions.ExtensionHandlerJavaClass.callFunction(ExtensionHandlerJavaClass.java:396) 
> 
>        ...
> 
> Firstly, I was expecting the Class to be "org.w3c.dom.Node", so does 
> anyone know how I can enable this? 

DTMNodeProxy is the concrete implementation class, so it makes sense 
that you get that.  org.w3c.dom.Node would just be one of the (possibly 
many) interfaces it implements so I wouldn't expect getClass().getName() 
to return it.  I think what you're really wanting is node.getNodeType() 
which returns one of the node type constants (see the Node javadoc).

> I was also expecting the name of the 
> first child to be "gml:exterior" instead of a "#text".  The XML I'm 
> sending is given below:
> 
>    <gml:Polygon gml:id="GATS1153_10">
>      <gml:exterior>
>        <gml:LinearRing>
>          <gml:posList srsDimension="2">-155.42 55.58</gml:posList>
>        </gml:LinearRing>
>      </gml:exterior>
>    </gml:Polygon>

My guess is that the first child of gml:Polygon is actually the 
whitespace text node before the gml:exterior element.  Pretty easy to 
test by looking at the length of getChildNodes() which would be 3.  To 
get straight to the gml:exterior you could use getElementsByTagNameNS, 
or check each child node's nodeType for ELEMENT_NODE.

> 
> Is it possible I actually need to pass a tree fragment, rather than a 
> node-set?
> 
> As a reminder, the XSLT given below.  Any help would me greatly 
> appreciated.  In simple terms, I'm just trying to pass the above XML 
> from XSLT to Java, preferably as a w3c Node.
> 
> Kind regards,
> 
> Andy
> 
> The XSLT:
> 
>  <xsl:stylesheet ... myClass="xalan://my.package.name.MyClass">
>    ...
>    <xsl:variable name="gmlAdder" select="myClass:new()"/>
> 
>    <xsl:variable name="gml">
>      <xsl:copy-of select="@* | node()"/>
>    </xsl:variable>
> 
>    <xsl:variable name="result" select="myClass:addGML($gmlAdder,$gml)"/>
>    ...
>  </xsl:stylesheet>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Passing a node-set from XSLT to Java

Posted by Andrew Chamberlain <an...@comsine.co.uk>.
Hi Luca/All,

Sorry.  On further testing, we're still not getting through the full 
tree fragment to Java.

I've adapted the java method to give details of what it receives:

  public String addGML(org.w3c.dom.Node myNode)
  {
       System.out.println("Class = "+node.getClass().getName());
       System.out.println("Name = "+node.getNodeName());
       System.out.println("Has children = "+(node.hasChildNodes()?"Yes":"No"));
       System.out.println("First Child Name = "+node.getFirstChild().getNodeName());
       System.out.println("First Child Text = "+node.getFirstChild().getTextContent());
  }

and from this comes the following output:

  Class = org.apache.xml.dtm.ref.DTMNodeProxy
  Name = gml:Polygon
  Has children = Yes
  First Child Name = #text

The last line (which uses the getTextContent() method) throws the 
following exception:

javax.xml.transform.TransformerException: java.lang.AbstractMethodError: 
org.apache.xml.dtm.ref.DTMNodeProxy.getTextContent()Ljava/lang/String;
        at 
org.apache.xalan.extensions.ExtensionHandlerJavaClass.callFunction(ExtensionHandlerJavaClass.java:396)
        ...

Firstly, I was expecting the Class to be "org.w3c.dom.Node", so does 
anyone know how I can enable this?  I was also expecting the name of the 
first child to be "gml:exterior" instead of a "#text".  The XML I'm 
sending is given below:

    <gml:Polygon gml:id="GATS1153_10">
      <gml:exterior>
        <gml:LinearRing>
          <gml:posList srsDimension="2">-155.42 55.58</gml:posList>
        </gml:LinearRing>
      </gml:exterior>
    </gml:Polygon>

Is it possible I actually need to pass a tree fragment, rather than a 
node-set?

As a reminder, the XSLT given below.  Any help would me greatly 
appreciated.  In simple terms, I'm just trying to pass the above XML 
from XSLT to Java, preferably as a w3c Node.

Kind regards,

Andy

The XSLT:

  <xsl:stylesheet ... myClass="xalan://my.package.name.MyClass">
    ...
    <xsl:variable name="gmlAdder" select="myClass:new()"/>

    <xsl:variable name="gml">
      <xsl:copy-of select="@* | node()"/>
    </xsl:variable>

    <xsl:variable name="result" select="myClass:addGML($gmlAdder,$gml)"/>
    ...
  </xsl:stylesheet>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Passing a node-set from XSLT to Java

Posted by Luca Morandini <lm...@ieee.org>.
Andrew Chamberlain wrote:
> Hi All,
> 
> I'm trying to pass a tree fragment from XSLT to Java in the following 
> way, and I'm not sure if the node-set is getting through correctly.  The 
> XML is:
...
> 
> This gives the 'result' variable the value of "#text", but when I was 
> expecting "gml:Polygon".

Not all XML fragments are nodesets in XSLT 1.0 (XSLT 2.0 is way better 
in this respect), hence you could try using the node extension of Xalan.

I haven't tried passing nodeset() to Java, but I presume this example 
may help you:

Pass this expression: xalan:nodeset($y-vector)

Where y-vector comes from an ordinary XML fragment like this:
   <xsl:template name="get-y-vector">
     <xsl:param name="geom"/>
     <vector>
       <xsl:for-each select="str:tokenize($geom, ' ')">
       <xsl:if test="string-length(substring-before(., ','))!=0">
         <y><xsl:value-of select="substring-before(., ',')"/></y>
         </xsl:if>
       </xsl:for-each>
     </vector>
   </xsl:template>

Regards,

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org