xsl-list
[Top] [All Lists]

Re: [xsl] Nested element move to single element

2010-01-30 07:42:08
Well, I think the following XSLT2 (which may as well be XSLT1) does
what I think you asked for. Putting aside the question "why?", it
still may not be what you really want, though, depending on how
consistent your input is.

Others on the list may well have better ways to do this, of course.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Note: stylesheet completely ignores nodes other -->
<!-- than elements and text (i.e., attributes, comments, -->
<!-- and processing instructions are dropped) -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  
  <!-- match only root <body> element -->
  <xsl:template match="/body">
    <!-- spit out copy of self -->
    <xsl:copy>
      <!-- working on child nodes as output children -->
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- when I hit an input <item-group>, put out a -->
  <!-- copy of same, but process only <item> and <sub-item> -->
  <!-- children, in that order. -->
  <xsl:template match="item-group">
    <xsl:apply-templates select="item"/>
    <xsl:apply-templates select="sub-item"/>
  </xsl:template>

  <!-- <item>s and <reference-targets> just get copied to -->
  <!-- output in a usual identity kinda way -->
  <xsl:template match="item|reference-targets">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- all <sub-item> and <sub-itemN> elements become an -->
  <!-- output <sub-item> that contains the input elements -->
  <!-- text content followed by what was the input element's -->
  <!--  younger sibling (now the output element's 2nd child) -->
  <xsl:template match="sub-item|sub-item1|sub-item2">
    <sub-item>
      <xsl:apply-templates select="text()"/>
      <xsl:apply-templates select="following-sibling::*[1]"/>
    </sub-item>
  </xsl:template>

</xsl:stylesheet>

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