Re: [xsl] Positional grouping problem

Subject: Re: [xsl] Positional grouping problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 01 Feb 2013 13:57:59 -0500
At 2013-02-01 13:40 -0500, Mark Peters wrote:
I've trying to group certain nodes in a flat MediaWiki ML file to
allow readers to show and hide the content. The grouping is based on
font sizes declared in a @style attribute, which have a uniform
structure across all content files.

Essentially, I'm trying to group each <div/> node where the @style
contains "14pt" with its following siblings. Some of the siblings have
other font sizes smaller than 14pt; others have no declared font sizes
because they wrap other nodes, such as tables.

You were really close.


What you weren't checking is that when the group does not match the grouping criteria for group-starting-with, then you are in the content before the first group.

I hope the solution below helps ... if I haven't missed seeing anything then the output matches what you said you wanted.

. . . . . . . . Ken


T:\ftemp>type mark.xml <?xml version="1.0" encoding="UTF-8"?> <div style="margin-bottom: 15pt; margin-left: 20pt;" xmlns="http://www.w3.org/1999/xhtml";> <div style="blah" /> <div style="font-family: sans-serif; font-size: 10pt;"> <span>Text</span> </div> <div style="font-size: 17pt;"> <span>Page Title</span> </div> <hr /> <div style="font-size: 14pt;"> <span>Title</span> </div> <div style="font-family: sans-serif; font-size: 10pt;"> <span>Text</span> </div> <div style="margin-left: 5pt;"> <table /> </div> <div style="font-family: sans-serif; font-size: 10pt;"> <span>Text</span> </div> <div style="font-size: 14pt;"> <span>Title</span> </div> <div style="font-family: sans-serif; font-size: 10pt;"> <span>Text</span> </div> <div style="font-family: sans-serif; font-size: 10pt;"> <span>Text</span> </div> <div style="margin-left: 5pt;"> <table /> </div> </div>

T:\ftemp>call xslt2 mark.xml mark.xsl
<div xmlns="http://www.w3.org/1999/xhtml";
     style="margin-bottom: 15pt; margin-left: 20pt;">
   <div style="blah"/>
   <div style="font-family: sans-serif; font-size: 10pt;">
        <span>Text</span>
    </div>
   <div style="font-size: 17pt;">
        <span>Page Title</span>
    </div>
   <hr/>
   <div class="NavFrame">
      <div class="NavHead">Title</div>
      <div class="NavContent">
         <div style="font-family: sans-serif; font-size: 10pt;">
            <span>Text</span>
         </div>
         <div style="margin-left: 5pt;">
            <table/>
         </div>
         <div style="font-family: sans-serif; font-size: 10pt;">
            <span>Text</span>
         </div>
      </div>
   </div>
   <div class="NavFrame">
      <div class="NavHead">Title</div>
      <div class="NavContent">
         <div style="font-family: sans-serif; font-size: 10pt;">
            <span>Text</span>
         </div>
         <div style="font-family: sans-serif; font-size: 10pt;">
            <span>Text</span>
         </div>
         <div style="margin-left: 5pt;">
            <table/>
         </div>
      </div>
   </div>
</div>

T:\ftemp>type mark.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:html="http://www.w3.org/1999/xhtml";
  xmlns="http://www.w3.org/1999/xhtml";
  exclude-result-prefixes="html"
  version="2.0">

<xsl:output indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="html:div">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:for-each-group select="*"
           group-starting-with="html:div[contains(@style,'14pt')]">
      <xsl:choose>
        <xsl:when test="self::html:div[contains(@style,'14pt')]">
          <!--found a group-->
          <div class="NavFrame">
            <div class="NavHead">
              <xsl:copy-of select="html:span/node()"/>
            </div>
            <div class="NavContent">
              <xsl:copy-of select="current-group()[position()>1]"/>
            </div>
          </div>
        </xsl:when>
        <xsl:otherwise>
          <!--this is the pre-amble before the first group-->
          <xsl:copy-of select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>rem Done!


-- Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread