p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 (http://p2p.wrox.com/forumdisplay.php?f=398)
-   -   xsl:copy-of Issue (http://p2p.wrox.com/showthread.php?t=71496)

DKatSSA November 17th, 2008 12:29 PM

xsl:copy-of Issue
 
I am having some trouble using xsl:copy-of and I am hoping you good folks would be able to help me. First, some background information on what I am attempting. I am trying to grab a node set from the XML file I am working with and pass it over to a Java function for processing in a XSL-FO processor. What it looks like is happening is when I pass the value it comes out as a string (as if I were using xsl:value-of instead of xsl:copy-of). I know this because when I set the parameter in Java to String, I get values (string content like xsl:value-of), but when I change the parameter data type to a NodeList it tells me the table is null. I am not sure what I am doing wrong here. Here is a clip of the code:


<xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:templateId/@root='2.16.840.1.113883.10.20.1.11']/n1:text/n1:table" >

  <fo:block>
      <xsl:copy-of select="javaRef:parseXMLtoDom(.)" />
  </fo:block>
</xsl:for-each>

Any ideas? Thanks!

David J. Katz


mhkay November 17th, 2008 01:06 PM

The behaviour of extension functions in XSLT is entirely implementation-defined, so you first need to tell us which XSLT processor you are using (I guess it's Xalan or Saxon, since the other three Java processors seem to be pretty-well defunct nowadays). It would also be useful to see your Java code.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

DKatSSA November 17th, 2008 02:40 PM

I am using Xalan 2.7.0. For the time being, I am just passing in the value and trying to display the result on the console before moving into the next steps of the program. Below is the code I am using if I am trying to display the resulting value as a string (which gives me string values without their tags):

 public void parseXMLtoDom (String xmlTextNode)throws Exception
    {
     System.out.println(xmlTextNode.toString());
    }

The following code will compile, but I get a "table:[null]" on the console when I try to display the first node in the index. This leads me to believe that XML is not being passed into the Java method:

 public void parseXMLtoDom (NodeList xmlTextNode)throws Exception
    {
         System.out.println(xmlTextNode.item(0));
    }

Thank you for the assistance!


mhkay November 17th, 2008 04:20 PM

I don't actually know how Xalan handles a Java method whose result type is declared as void, but since you are calling a method that doesn't return anything, and then trying to copy what it returns, I wouldn't expect anything very useful to happen as a result.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

DKatSSA November 17th, 2008 04:48 PM

It is true that the method is not yet returning a value, but I am able to pass in String values from the XSL and display them on the console via System.out.println, so it is doing something. Let me back this problem out a step. I tried thr following XSL code and found that "Hello World" displayed, but nothing else.


<xsl:for-each select="/n1:ClinicalDocument/n1:component/n1:structuredBody/n1:component/n1:section[n1:templateId/@root='2.16.840.1.113883.10.20.1.11']/n1:text/n1:table" >

<fo:block>
    <xsl:copy-of select="." />
    <xsl:text>Hello World!</xsl:text>
 </fo:block>

</xsl:for-each>

Just to make sure the FO processor was not the culprit, I ran this same XSL code (sans the FO tags) and came up with the same result. Any ideas?


mhkay November 17th, 2008 05:00 PM

>I tried thr following XSL code and found that "Hello World" displayed, but nothing else.

Difficult without seeing your source document (and since it looks like HL7 I would really rather not see it, it would spoil my evening). On the surface, the xsl:for-each has selected an element, the xsl:copy-of must therefore copy the element. So I think it has to be the conclusion: the bug is not in the code you have shown us, so it must be somewhere else. Your next step is to put together a demonstration of the problem that is complete enough for anyone to run, but concise enough to make that a practical proposition. At least you've now eliminated the extension function call as irrelevant to the problem.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

DKatSSA November 18th, 2008 12:42 PM

Here is a simple example where I'm taking Java and Xalan out of the picture. I am using XML Notepad to do the transformation. Here is the XML:

     <AAA >
          <BBB ooo = "111" ppp = "222" >
               <CCC>c1 </CCC>
               <CCC>c2 </CCC>
           </BBB>
          <BBB ooo = "555" ppp = "666" >
               <DDD>d1 </DDD>
               <DDD>d2 </DDD>
           </BBB>
      </AAA>

XSL:

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >

<xsl:output method = "xml" indent = "yes" />

    <xsl:template match = "/" >

         <xsl:copy-of select = "//BBB" />

    </xsl:template>
</xsl:stylesheet>

I am expecting to see the node-set displayed (starting at BBB), but instead in the transformation window I get: c1 c2 d1 d2. This is the result I would expect using xsl:value-of. This is as simple as it gets, so I am wondering if perhaps I am not understanding what this function should be doing?


mhkay November 19th, 2008 11:26 AM

I suspect you are viewing the result in a "browser" view, where it is treating the result tree as HTML. You need to look at it in an "XML" view, where you can see the markup. It's a while since I used XML Notepad and it's not installed on my current machine, but I'm sure it has option settings for how the output is viewed.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

DKatSSA November 19th, 2008 05:18 PM

I was able to get this working but I had to do a little home work with Xalan. I added

xmlns:HitMerExtractFoReport="xalan://src.HitMerExtractFoReport"

to the XSL header and changed the function to

<xsl:variable name="success" select="HitMerExtractFoReport:parseXMLtoDom(*)"/>

This passed the node set into Java as a NodeList/NodeSet/NodeIterator. Not my first choice of classes to work with when it comes to HL7 v3, but it will have to do. Thank you for the input.



All times are GMT -4. The time now is 11:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.