[xsl] Creating a container element for siblings which have different start and end elements

Subject: [xsl] Creating a container element for siblings which have different start and end elements
From: "Michael Friedman sumarimike@xxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 29 Jan 2015 16:25:06 -0000
Greetings,
 
I have some XML which has certain elements Id like to move into a containing
element. Im having trouble.
 
The current XML is arranged this way:
<para>Planes have wings so they can <tech:revst
type="Airline"/>fly<tech:revend/> in the sky and get us to places wed like to
visit faster <tech:revst type="Airline"/>than if wed walked there.<caution>
<para>Please stay in the airplane at all
times</para></caution><tech:revend/></para>
 
In this older method of indicating a revision markup area, <tech:revst/> is
used to indicate a start of a change and <tech:revend> is used to indicate the
end. There can be multiple instances of this revision markup anywhere,
surrounding text within elements, or surrounding multiple elements and text.
These containers produce change bars in PDF and background-color areas in
HTML.
 
I am producing PDF and HTML output. In order to create a <span> or <div> in
HTML output, Id like to pre-process the XML to produce the following desired
XML output structure.
 
Desired output:
<para>Planes have wings so they can <tech:revst
type="Airline">fly</tech:revst><tech:revend/> in the sky and get us to places
wed like to visit faster <tech:revst type="Airline">than if wed walked
there.<caution>
<para>Please stay in the airplane at all
times</para></caution></tech:revst><tech:revend/></para>
 
Essentially, Id like to encapsulate the siblings between the <tech:revst/>
and <tech:revend/> into the <tech:revst>. The <tech:revend/> is left in for FO
processing.
 
Using XSLT 2.0, Ive been able to get the first <tech:revst> in a given
sibling structure partially handled. However, the remaining ones (in this
case) are omitted. Im using saxon 6 via our publishing app to do the heavy
lifting.
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        xmlns:tech="http://www.techpubsglobal.com/";
                        version="2.0">
                       
    <xsl:output method="xml" indent="no" doctype-public="-//Tech//DTD
FlightBook XML V4.0//EN" doctype-system="flightbook.dtd"/>

    <xsl:template match="/|*|comment()|processing-instruction()">
       <xsl:call-template name="do_copy"/>
    </xsl:template>

    <xsl:template match="tech:revst">
            <xsl:element name="tech:revst">
                  <xsl:for-each select="@*">
                        <xsl:attribute name="{name(.)}">
                  <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
                  <xsl:for-each
select="following-sibling::*[not(self::tech:revend)][not(preceding-sibling::t
ech:revend)]|following-sibling::text()[preceding-sibling::tech:revst][followi
ng-sibling::tech:revend]">
                        <xsl:copy>
                        <xsl:for-each select="@*">
                                    <xsl:copy/>
                              </xsl:for-each>
                              <xsl:apply-templates/>
                    </xsl:copy>
                  </xsl:for-each>
            </xsl:element>
      </xsl:template>

      <xsl:template
match="*[preceding-sibling::tech:revst][following-sibling::tech:revend]"/>
      <xsl:template
match="text()[preceding-sibling::tech:revst][following-sibling::tech:revend]"
/>
    
      <xsl:template name="do_copy">
          <xsl:copy>
            <xsl:for-each select="@*">
                        <xsl:copy/>
                  </xsl:for-each>
                  <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
                       
</xsl:stylesheet>
 
The following is produced from this stylesheet:
<para>Planes have wings so they can <tech:revst type="Airline">fly in the sky
and get us to places wed like to visit faster than if wed walked
there.</tech:revst><tech:revend/></para>
 
I found some useful suggestions on grouping on the dpawson.co.uk site, but
each one I tried didnt quite get me what I was looking for. In particular,
there was a Working with pairs of siblings that used sequences, but I still
couldnt get that to work when there were multiple <tech:revst/><tech:revend/>
pairs in a set of siblings.
 
Suggestions?
 
Thanks,
Michael Friedman

Current Thread