xsl-list
[Top] [All Lists]

RE: [xsl] Selecting everything "before" and "after" a specific node

2009-04-02 13:39:37
Thanks David - this has got me so close!
I'm doing reading to understand how your stuff works(!) and how to adapt it to 
my real-life needs, but thought I'd post a reply for any further input

To get a bit closer to my real-life situation, my source XML is more like 
this...

<A i="1">
      <STUFF/>
        <MORE_STUFF/>
        <WRAPPER>
                <B i="2">
                <C i="3"/>
        </B>
        <B i="4">
                <C i="5">
                        <D i="6"/>
                        <D i="7">
                                <E i="8"/>
                                <E i="9" msg="get me out of here!"/>
                                <E i="10"/>
                        </D>
                        <D i="11"/>
                </C>
                <C i="12"/>
        </B>
        </WRAPPER>
</A>

And my output more like this
<A i="1">
   <STUFF/>
   <MORE_STUFF/>
   <WRAPPER>
        <BEFORE>
         <B i="2">
            <C i="3"/>
         </B>
         <B i="4">
            <C i="5">
               <D i="6"/>
               <D i="7">
                  <E i="8"/>
               </D>
            </C>
         </B>
        </BEFORE>
        <PULLED>
        <E i="9" msg="get me out of here!"/>
        </PULLED>
        <AFTER>
        <B i="4">
                <C i="5">
                <D i="7">
                                <E i="10"/>
                </D>
                <D i="11"/>
                </C>
        <C i="12"/>
            </B>
         </AFTER>
   </WRAPPER>
</A>

So I'm trying to use an identity template to kick of the processing.
Then I have a template to match WRAPPER, which contains your stuff, only I'm 
attempting to constraint it operate on the value of the variable $sub-tree, 
defined as:

<xsl:variable name="sub-tree" select="/A/WRAPPER"/>

<xsl:template match="WRAPPER">
        <WRAPPER>
                <BEFORE>
                        <xsl:apply-templates select="$sub-tree" mode="before"/>
                <BEFORE>
                <PULLED>                
                        <xsl:copy-of select="$this"/>
                <PULLED>
                <AFTER>
                        <xsl:apply-templates select="$sub-tree" mode="after"/>
                </AFTER>
        </WRAPPER>
</xsl:template>

But here my lack of understanding kicks in - this doesn't work (which doesn't 
surprise me!) and here's where I need to bolster my knowledge...bit if you did 
have a quick input in the meantime that would really be appreciated.

Thanks so much

-----Original Message-----
From: David Carlisle [mailto:davidc(_at_)nag(_dot_)co(_dot_)uk] 
Sent: Thursday, April 02, 2009 7:51 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Selecting everything "before" and "after" a specific node



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >
 
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:variable name="this" select="//E[(_at_)i='9']"/>

<xsl:template match="/">
 <ROOT>
  <BEFORE>
   <xsl:apply-templates mode="before"/>
  </BEFORE>
  <PULLED>
   <xsl:copy-of select="$this"/>
  </PULLED>
  <AFTER>
   <xsl:apply-templates mode="after"/>
  </AFTER>
 </ROOT>
</xsl:template>


<xsl:template match="*[. is $this]" mode="before after" priority="3"/>
<xsl:template match="*[. &gt;&gt; $this]" mode="before" priority="2"/>

<xsl:template match="*" mode="before after">
 <xsl:copy>
  <xsl:copy-of  select="@*"/>
  <xsl:apply-templates mode="#current"/>
 </xsl:copy>
</xsl:template>


<xsl:variable name="before" select="$this/preceding::*"/>

<xsl:template match="*[$before[. is current()]]" mode="after" priority="2"/>



</xsl:stylesheet>



$ saxon9 pull.xml pull.xsl
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
   <BEFORE>
      <A i="1">
         <B i="2">
            <C i="3"/>
         </B>
         <B i="4">
            <C i="5">
               <D i="6"/>
               <D i="7">
                  <E i="8"/>
               </D>
            </C>
         </B>
      </A>
   </BEFORE>
   <PULLED>
      <E i="9" msg="get me out of here!"/>
   </PULLED>
   <AFTER>
      <A i="1">
         <B i="4">
            <C i="5">
               <D i="7">
                  <E i="10"/>
               </D>
               <D i="11"/>
            </C>
            <C i="12"/>
         </B>
      </A>
   </AFTER>
</ROOT>

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

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