[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Can you break one node tree into two?


Subject: Re: [xsl] Can you break one node tree into two?
From: JBryant@xxxxxxxxx
Date: Mon, 31 Jan 2005 16:47:33 -0600

Hi, Becky,

At its most abstract, this is a grouping problem (arbitrarily breaking a 
collection of elements at some given point), so you should see Jeni 
Tennison's grouping goodies at 
http://www.jenitennison.com/xslt/grouping/index.html

Also, in XML, you do need to have everything wrapped in a single element 
(the document root), so the first output you show wouldn't be XML. On the 
other hand, you can use the result-document function if you don't mind 
multiple files and can use XSL 2 (which pretty much means using Saxon).

If you ALWAYS get the SAME structure you show here, it gets a lot easier. 
If you really can rely on the same structure all the time, you can do 
something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" 
indent="yes"/>

  <xsl:template match="RootEle">
    <NewRoot>
      <RootEle>
        <Letter>
          <xsl:copy-of select="Letter/From"/>
          <xsl:copy-of select="Letter/To"/>
          <xsl:copy-of select="Letter/Address"/>
        </Letter>
      </RootEle>
      <RootEle>
        <Letter>
          <xsl:copy-of select="Letter/Subject"/>
          <xsl:copy-of select="Letter/Body"/>
        </Letter>
      </RootEle>
    </NewRoot>
  </xsl:template>

</xsl:stylesheet>

I tested that on Saxon with your input and got the second of your desired 
outputs. As I said, though, the real fun comes when you can't rely on the 
same structure always being present. For that, I defer to the list's gurus 
(I make no claim to being an expert; I just use XSL to solve my own weird 
problems).

HTH

Jay Bryant
Bryant Communication Services
(on contract at Synergistic Solution Technologies)





"Wilde Rebecca L SSgt HQ SSG/STS" <Rebecca.Wilde@xxxxxxxxxxxxx> 
01/31/2005 03:36 PM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
<xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
cc

Subject
[xsl] Can you break one node tree into two?






Hello,

I'm trying to take some XML such as:

<RootEle xmlns="">
  <Letter>
      <From/>
      <To/>
      <Address/>
      <Subject/>
      <Body/>
  </Letter>
</RootEle>

And I would like my XSLT to output:

<RootEle xmlns="">
   <Letter>
       <From/>
       <To/>
       <Address/>
   </Letter>
</RootEle>
<RootEle xmlns="">
    <Letter>
        <Subject/>
        <Body/>
    </Letter>
</RootEle>

Basically I want to say as soon as I see the Address node I want to
break it out and everything above it into one node tree and everything
below it into a second node tree.  The nodes could be anything, but if
an Address node is passed to me, I need to break the node tree into two.

I am think I need to do something with the xsl:copy-of and the
xsl:for-each, but my xslt knowledge is very limited and attempting to
use this is not creating anything near what I had hoped for.

If it isn't possible to return two node trees (which I suspect it
isn't),  how would I make it look like: <NewRoot>
  <RootEle xmlns="">
    <Letter>
       <From/>
       <To/>
       <Address/>
    </Letter>
  </RootEle>
  <RootEle xmlns="">
    <Letter>
        <Subject/>
        <Body/>
    </Letter>
  </RootEle>
</NewRoot>

Thank you,
Becky


Current Thread
Keywords