xsl-list
[Top] [All Lists]

Re: [xsl] Reordering elements

2006-06-13 14:23:41
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

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

<Prev in Thread] Current Thread [Next in Thread>