xsl-list
[Top] [All Lists]

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

2005-11-09 09:27:51

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>
--~--