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

Re: [xsl] Reordering elements


Subject: Re: [xsl] Reordering elements
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Tue, 13 Jun 2006 23:23:19 +0200

Chad Chelius wrote:

In a situation where my XML file looks like this:


<Root>
    <Story>
    <Source>
</Root>

How would I move the <Source> element so that and it's children are
now a child of <Story>?


It depends. I assume you have something like
 <Root>
  <Story>
    some content
  </Story>
  <Source>
    some source stuff
  </Source>
  <Story>
    some more content
  </Story>
  <Source>
    some more source stuff
  </Source>
 <Root>
with one and only one Source element following a Story element.

and you want this transformed into
 <Root>
  <Story>
    some content
    some source stuff
  </Story>
  <Story>
    some more content
    some more source stuff
  </Story>
 <Root>

Then the following should work:
  <xsl:template match="Story">
   <Story>
     <!-- first copy all original children of the Story element -->
     <xsl:copy-of select="node()"/>
     <!-- then copy all children of the following Source element -->
     <xsl:copy-of select="following-sibling::Source[1]/node()"/>
   </Story>
  </xsl:template>
  <!-- suppress Source elements -->
  <xsl:template match="Source"/>
You'll need to process the Root element, and you may want to apply
other methods in order to suppress processing the original Source
elements.
And beware: the code above is completely untested.

J.Pietschmann


Current Thread
Keywords
xml