xsl-list
[Top] [All Lists]

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

2015-01-29 10:25:20
Greetings,
 
I have some XML which has certain elements I’d like to move into a containing 
element. I’m 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 we’d like to 
visit faster <tech:revst type="Airline"/>than if we’d 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, I’d 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 
we’d like to visit faster <tech:revst type="Airline">than if we’d walked 
there.<caution>
<para>Please stay in the airplane at all 
times</para></caution></tech:revst><tech:revend/></para>
 
Essentially, I’d 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, I’ve been able to get the first <tech:revst> in a given sibling 
structure partially handled. However, the remaining ones (in this case) are 
omitted. I’m 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::tech:revend)]|following-sibling::text()[preceding-sibling::tech:revst][following-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 we’d like to visit faster than if we’d 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 didn’t quite get me what I was looking for. In particular, there 
was a “Working with pairs of siblings” that used sequences, but I still 
couldn’t get that to work when there were multiple <tech:revst/><tech:revend/> 
pairs in a set of siblings.
 
Suggestions?
 
Thanks,
Michael Friedman                                          
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

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