RE: [xsl] parsing a complex xml tree into simple trees (a challenging problem at least for me :)

Subject: RE: [xsl] parsing a complex xml tree into simple trees (a challenging problem at least for me :)
From: "daghan" <daghanacay@xxxxxxxxxxx>
Date: Fri, 11 Nov 2005 16:09:46 +1100
Hi there;

First of all thank you for your timely response. However I have some more
problems. First of all it is not working with Java-Xalan processor and
following is the error message without any clue about the line number the
error was thrown;

" Starting 
Using - default
JRE XSLT processor.
Invalid conversion from 'java.lang.String' to 'node-set'.
Transformation finished."

The problem is String to node-set conversion. (by the way u were right
Java-Xalan supports exslt:node-set()). I have changed the
"text()[contains(.,',')][1]" to " descendant::text()[contains(.,',')][1]" as
you have recommended. I have also changed <xsl:when test="$a"> to <xsl:when
test="boolean($a)"> in case it was the problem. You have mentioned that it
works with Saxon may be I should upgrade to Saxon too but my concern is the
reusability of the code and transportability on all JRE. 

Apart from that problem I have two more questions to ask;

1- if I am not mistaken the recursion in your code happens on tree fragment
by converting it to node-set() that is clever I was not very accustomed to
use variables in this way, I am a newbie :) but what if I have more than one
chops in my source XML an in addition to that different elements that I have
no idea of before the processing such as;. 
    <chops att1="something">1,2<X>XText1,XText2<Y>YT1,YT2</Y></X></chops>
    <chops att1="anything">2<K>n,k,l</K></chops>

I was solving it using xsl:copy in my case I believe I can do it with your
code too but I am little confused about your implicit recursion.

2- Is there any book or resource you may recommend me to learn the tricks of
XSLT 1.0.  In addition to that do you recommend me to upgrade to Saxon and
learn XSLT 2.0 is there major benefit of learning and installing it compared
to losing the transportability of the code over JREs. I am already using
DOM4J for internal manipulation of DOM tree in java upgrading to Saxon is
also very expensive since I have put some effort to learn DOM4J and my Java
code and internal manipulation of DOM tree should be re-written based on
Saxon. Do you see why I was so stubborn to stick with the Java-Xalan?

Thank you very much even this much of code snippet show me new horizons. I
was struggling in my little world of XSLT and trying to learn it form the
documentation but I was not as inventive as this one.


-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx] 
Sent: Thursday, 10 November 2005 3:28 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] parsing a complex xml tree into simple trees (a
challenging problem at least for me :)

I think that would be quite hard to do without any extension at all, the
following probably does what you want, and uses the node-set extension
which most processors support (apart from mozilla). I used the exslt
namespace as I think xalan supports that, although I tested this with
saxon. This would probably be easier in xslt2.

    <chops att1="something">1,2<X>XText1,XText2<Y>YT1,YT2</Y></X></chops>

<xsl:stylesheet xmlns:xsl=""; 

<xsl:template match="root">

<xsl:template match="chops">
 <xsl:param name="a" select=".//text()[contains(.,',')][1]"/>
 <xsl:param name="i" select="generate-id($a)"/>
   <xsl:when test="$a">
     <xsl:variable name="c">
    <xsl:copy-of select="@*"/>
       <xsl:apply-templates mode="c">
         <xsl:with-param name="i" select="$i"/>
         <xsl:with-param name="r"
      <xsl:if test="contains($a,',')">
      <xsl:apply-templates select=".">
	<xsl:with-param name="i" select="$i"/>
	<xsl:with-param name="a" select="substring-after($a,',')"/>
     <xsl:apply-templates select="exslt:node-set($c)/chops"/>
     <xsl:copy-of select="."/>
<xsl:template match="node()" mode="c">
<xsl:param name="i"/>
<xsl:param name="r"/>
 <xsl:copy-of select="@*"/>
  <xsl:apply-templates mode="c">
         <xsl:with-param name="i" select="$i"/>
         <xsl:with-param name="r" select="$r"/>

<xsl:template match="text()" mode="c" priority="2">
<xsl:param name="i"/>
<xsl:param name="r"/>
 <xsl:when test="generate-id()=$i"><xsl:value-of select="$r"/></xsl:when>
 <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>



$ saxon chops.xml chops.xsl
<?xml version="1.0" encoding="utf-8"?><root> 
<chops att1="something">1<X>XText1<Y>YT1</Y></X></chops>
<chops att1="something">1<X>XText1<Y>YT2</Y></X></chops>
<chops att1="something">1<X>XText2<Y>YT1</Y></X></chops>
<chops att1="something">1<X>XText2<Y>YT2</Y></X></chops>
<chops att1="something">2<X>XText1<Y>YT1</Y></X></chops>
<chops att1="something">2<X>XText1<Y>YT2</Y></X></chops>
<chops att1="something">2<X>XText2<Y>YT1</Y></X></chops>
<chops att1="something">2<X>XText2<Y>YT2</Y></X></chops>


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

Current Thread