[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: parsing a complex xml tree into simple trees (a c

Subject: Re: parsing a complex xml tree into simple trees (a challenging problem at least for me :)
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 9 Nov 2005 16:27:51 GMT
trees in c
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
________________________________________________________________________

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.