[xsl] text node question

Subject: [xsl] text node question
From: Marji_Berkman@xxxxxxxx
Date: Wed, 18 Apr 2001 13:48:14 -0400
This contains a few questions, any help with any part would be 
appreciated. 

I'm trying to transform this xml using xalan: 

XML:
<richtext>
  <pardef id='1'/>
  <par def='1'>
    This is the first paragraph.  The outer hotspot starts
    <hotspot regionid='1'>
      <code>
        ...
      </code>
      <run>
        here and continues into the next paragraph.
      </run>
    </hotspot>
  </par>
  <par def='1'>
    <region regionid='1'>
      This is the second paragraph
      <objectref regionid='2'>
        <run>
          Hello, world.
        </run>
      </objectref>
    </region>
  </par>
  <pardef id='2'/>
  <par def='2'>
    <region regionid='1'>
      Text in region with regionid 1 BEFORE child region element with 
regionid 2 
      <region regionid='2'>
        <run>
          Goodbye, world.
        </run>
      </region>
           Text in region with regionid 1 AFTER child region element with 
regionid 2 
    </region>
    and now the sentence and paragraph end.
  </par>
</richtext> 

INTO THIS:
<hotspot> 
  <code> ... </code>
  <run> here and continues into the next paragraph.</run>
  This is the second paragraph 
  Text in region with regionid 1 BEFORE child region element with regionid 
2 
  <run> Hello, world.</run>
  <run> Goodbye, world.</run>
  Text in region with regionid 1 AFTER child region element with regionid 
2 
</hotspot>


I want to extract the hotspot and it's regions (all have the same unique 
regionid attribute), including the child element's children - but without 
the child element tags. 
And have been only partially successful, which means not successful at 
all!

When I use the follwoing xsl, I get the element <hotspot>, but I don't get 
the text which appears after the child region element ("Text in region 
with regionid 1 AFTER child region element with regionid 2")
XSL 1: 
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
version='1.0'>
      <xsl:template match='/'>
         <hotspot>
         <xsl:for-each select='//hotspot[@regionid]'> 
            <xsl:call-template name='firstid'/>
               <xsl:for-each select='//region'>
                   <xsl:call-template name='getregions'>
                     <xsl:with-param name='rid' select='@regionid'/>
                   </xsl:call-template> 
              </xsl:for-each>
        </xsl:for-each>
       </hotspot>
</xsl:template>

<xsl:template name='firstid'>
   <xsl:value-of select='text()'/>
     <xsl:for-each select='*'>
       <xsl:if test="not(@regionid)">
          <xsl:copy-of select='.'/>
      </xsl:if>
     </xsl:for-each>
</xsl:template>

<xsl:template name='getregions'>
  <xsl:param name='rid'/>
  <xsl:value-of select='text()'/>
  <xsl:for-each select='*'>
    <xsl:if test="not(@regionid='$rid')">
       <xsl:copy-of select='./*'/>
    </xsl:if>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

When I use the XSL 2, all the text appears, but not in the right place. 
(results follow the XSL)
 I include this, only because here it seems the text nodes of the region 
element with the regionid=1 are concatenated, but in the first example I 
only get the first part of the text node. ( I don't get the hotspot 
element because it didn't like where I was trying to insert the element 
tags - left this part out, because I don't think the structure of this xsl 
allows it.)

XSL 2:
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
version='1.0'>
     <xsl:template match='/'>
        <xsl:for-each select="//*[@regionid]/.">
          <xsl:sort select='@regionid'/>
           <xsl:choose>
               <xsl:when  test="name(.) = 'hotspot'"> 
                  <xsl:variable name='id' select='@regionid'/>
                 HOTSPOT:  <xsl:value-of select='$id'/>
                     <xsl:for-each select="child::*">
                           <xsl:copy-of select='.'/>
                      </xsl:for-each>
               </xsl:when> 
               <xsl:when  test="name(.) = 'region'"> 
                    <xsl:copy-of select='text()'/>
                    <xsl:for-each select="child::*"> 
                       <xsl:if test='not[@regionid="$id"]'>
                            <xsl:copy-of select='.'/>
                       </xsl:if> 
                      <xsl:for-each select="descendant::*"> 
                           <xsl:copy-of select='.'/> 
                      </xsl:for-each>
                   </xsl:for-each>
               </xsl:when>
           </xsl:choose>
       </xsl:for-each>
       </xsl:template>
</xsl:stylesheet>


RESULT OF TRANSFORM WITH XSL 2.
HOTSPOT: 1
<code> ... </code>
<run> here and continues into the next paragraph. </run> 
This is the second paragraph with an 
<run> Hello, world. </run> 
Text in region with regionid 1 BEFORE child region element with regionid 2 

Text in region with regionid 1 AFTER child region element with regionid 2 
<run> Goodbye, world. </run> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread