Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Harvey, Paul" <Paul_Harvey@----------->
To:
Date: 4/2/2009 5:40:00 PM
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@xxxxxxxxx]
Sent: Thursday, April 02, 2009 7:51 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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[@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.
________________________________________________________________________


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent