<?xml version="1.0" encoding="UTF-8"?>
<!--
  LEGAL INFORMATION
  
  Copyright (c) 2007 Syncro Soft Ltd  (http://www.oxygenxml.com)
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  
  * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer. 
  Altered source versions must be plainly marked as such, and must not be 
  misrepresented as being the original software.
  
  * Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
  distribution.
  
  * Neither the name of the Syncro Soft nor
  the names of its contributors may be used to endorse or promote
  products derived from this software without specific prior written
  permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:n="http://www.oxygenxml.com/nvdl"
  xmlns:nvdl="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0" exclude-result-prefixes="nvdl">
  <xsl:output indent="yes"/>
  <!-- The NVDL script, if relative then specify it relative to the XML file -->
  <xsl:param name="nvdl" select="/.."/>
  <!-- Select all the trigger elements from the NVDL script -->
  <xsl:variable name="triggers" select="document($nvdl, /)/nvdl:rules/nvdl:trigger"/>
  <!-- Process elements, if we detect a new section then we create an n:section element -->
  <xsl:template match="*">
    <!-- The trigger if the section was determined by a trigger and not by a change in namespace -->
    <xsl:param name="trigger" select="/.."/>
    <!-- Helper variables, the parent element namespace, the local name, the matching trigger -->
    <xsl:variable name="parentNamespace" select="namespace-uri(..)"/>
    <xsl:variable name="localName" select="local-name()"/>
    <xsl:variable name="matchingTrigger"
      select="$triggers[@ns=$parentNamespace and contains(@nameList, $localName) and generate-id($trigger)!=generate-id(.)]"/>
    <!-- Get the sections marked up content starting with this element -->
    <xsl:variable name="content">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <!-- Generates attribute sections -->
        <xsl:for-each-group select="@*" group-by="namespace-uri()">
          <xsl:attribute name="n:attSection{position()}">
            <xsl:value-of select="current-group()/name()"/>
          </xsl:attribute>
          <xsl:attribute name="n:attSection{position()}ns">
            <xsl:value-of select="namespace-uri()"/>
          </xsl:attribute>
        </xsl:for-each-group>
        <xsl:apply-templates select="node()">
          <xsl:with-param name="trigger" select="$matchingTrigger"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:variable>
    <!-- If a section starts here then mark that up with an n:section element else just copy the content as it is -->
    <xsl:choose>
      <xsl:when test="namespace-uri()!=$parentNamespace or $matchingTrigger or not(parent::*)">
        <n:section ns="{namespace-uri()}">
          <xsl:copy-of select="$content"/>
        </n:section>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$content"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
