xsl-list
[Top] [All Lists]

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

2005-11-10 22:09:46
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 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl - 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;. 
<root> 
    <chops att1="something">1,2<X>XText1,XText2<Y>YT1,YT2</Y></X></chops>
    <chops att1="anything">2<K>n,k,l</K></chops>
    <anotherElement>a,z<P></P></anotherElement>
          .
          .
          .
</root>

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.

Cheers

-----Original Message-----
From: David Carlisle [mailto:davidc(_at_)nag(_dot_)co(_dot_)uk] 
Sent: Thursday, 10 November 2005 3:28 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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.

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



<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="1.0"  
xmlns:exslt="http://exslt.org/common";
exclude-result-prefixes="exslt">


<xsl:template match="root">
<root>
  <xsl:apply-templates/>
</root>
</xsl:template>

<xsl:template match="chops">
 <xsl:param name="a" select=".//text()[contains(.,',')][1]"/>
 <xsl:param name="i" select="generate-id($a)"/>
 <xsl:choose>
   <xsl:when test="$a">
     <xsl:variable name="c">
     <xsl:text>&#10;</xsl:text>
     <chops>
    <xsl:copy-of select="@*"/>
       <xsl:apply-templates mode="c">
         <xsl:with-param name="i" select="$i"/>
         <xsl:with-param name="r"
select="substring-before(concat($a,','),',')"/>
       </xsl:apply-templates>
     </chops>
      <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>
      </xsl:if>
     </xsl:variable>
     <xsl:apply-templates select="exslt:node-set($c)/chops"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:text>&#10;</xsl:text>
     <xsl:copy-of select="."/>
   </xsl:otherwise>
 </xsl:choose>
</xsl:template>
 
<xsl:template match="node()" mode="c">
<xsl:param name="i"/>
<xsl:param name="r"/>
<xsl:copy>
 <xsl:copy-of select="@*"/>
  <xsl:apply-templates mode="c">
         <xsl:with-param name="i" select="$i"/>
         <xsl:with-param name="r" select="$r"/>
  </xsl:apply-templates>
</xsl:copy>
</xsl:template>

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


 

</xsl:stylesheet>




$ 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>
</root>



David

________________________________________________________________________
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:
http://www.star.net.uk
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--