Wednesday 23 May 2012

xsl:for-each-group @group-adjacent

Developed By:- Shailendra Srivastava


INPUT:-

<Contraindications>They need to know if you have any of these
  conditions:<br/>&#x2022;diabetes<br/>&#x2022;glaucoma<br/>&#x2022;head trauma<br/>&#x2022;heart
  disease<br/>&#x2022;high blood pressure<br/>&#x2022;if you have taken an MAOI like Carbex,
  Eldepryl, Marplan, Nardil, or Parnate in last 14 days<br/>&#x2022;lung or breathing disease,
  like asthma<br/>&#x2022;stomach or intestinal problems<br/>&#x2022;trouble passing
  urine<br/>&#x2022;an unusual or allergic reaction to codeine, hydrocodone, pseudoephedrine,
  other medicines, foods, dyes, or preservatives&#x2022;breast-feeding</Contraindications>

Expected Output:-

<?xml version="1.0" encoding="UTF-8"?>
<div id="div.0030" class="section_contraindications">
  <h1 id="h1.0015" class="section-title">What should I tell my health care provider before I take
    this medicine?</h1>
  <p id="p.0035" class="default">They need to know if you have any of these conditions:</p>
  <ul id="ul.0005" class="default">
    <li id="li.0005">diabetes</li>
    <li id="li.0010">glaucoma</li>
    <li id="li.0015">head trauma</li>
    <li id="li.0020">heart disease</li>
    <li id="li.0025">high blood pressure</li>
    <li id="li.0030">if you have taken an MAOI like Carbex, Eldepryl, Marplan, Nardil, or Parnate in
      last 14 days</li>
    <li id="li.0035">lung or breathing disease, like asthma</li>
    <li id="li.0040">stomach or intestinal problems</li>
    <li id="li.0045">trouble passing urine</li>
    <li id="li.0050">an unusual or allergic reaction to codeine, hydrocodone, pseudoephedrine, other
      medicines, foods, dyes, or preservativesbreast-feeding</li>
  </ul>
</div>


XSLT Code:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:pe="vishnu.com">
  
  <xsl:output method="xml" indent="no" use-character-maps="entity"/>
  
  <xsl:character-map name="entity"> 
    <xsl:output-character character="&#x2022;" string=""/> 
  </xsl:character-map>
  
  <xsl:template match="pe:Contraindications"> 
    <div id="div.0030" class="section_contraindications"> 
      <xsl:for-each-group select="child::node()" 
        group-adjacent="boolean(self::br or self::text()[not(local-name(preceding-sibling::node()[1]) = 'BR')])">
        <xsl:choose> 
          <xsl:when test="current-grouping-key()"> 
            <ul id="ul.0005" class="default"> 
              <xsl:for-each select="current-group()[self::text()]"> 
                <li> 
                  <xsl:attribute name="id"> 
                    <xsl:variable name="li-att1"> 
                      <xsl:number format="1" count="br" 
                        from="pe:Contraindications" level="any"/> 
                    </xsl:variable> 
                    <xsl:variable name="li-att2"> 
                      <xsl:value-of select="$li-att1 * 5"/> 
                    </xsl:variable> 
                    <xsl:variable name="li-att3"> 
                      <xsl:choose> 
                        <xsl:when test="$li-att2 = 5"> 
                          <xsl:value-of select="concat('li.000', $li-att2)" 
                          /> 
                        </xsl:when> 
                        <xsl:when test="$li-att2 &lt; 100"> 
                          <xsl:value-of select="concat('li.00', $li-att2)"/> 
                        </xsl:when> 
                        <xsl:when test="$li-att2 &lt; 1000"> 
                          <xsl:value-of select="concat('li.0', $li-att2)"/> 
                        </xsl:when> 
                        <xsl:otherwise> 
                          <xsl:value-of select="concat('li.', $li-att2)"/> 
                        </xsl:otherwise> 
                      </xsl:choose> 
                    </xsl:variable> 
                    <xsl:value-of select="$li-att3"/> 
                  </xsl:attribute> 
                  <xsl:copy/> 
                </li> 
              </xsl:for-each> 
            </ul> 
          </xsl:when> 
          <xsl:otherwise> 
            <xsl:apply-templates select="current-group()"/> 
          </xsl:otherwise> 
        </xsl:choose> 
      </xsl:for-each-group> 
    </div> 
  </xsl:template>
  
  <xsl:template match="U"> 
    <h1 id="h1.0015" class="section-title"> 
      <xsl:apply-templates/> 
    </h1> 
  </xsl:template>
  
  <xsl:template match="node()[name(preceding-sibling::node()[1]) = 'BR']"> 
    <xsl:if test="self::text()"> 
      <p id="p.0035" class="default"> 
        <xsl:value-of select="."/> 
      </p> 
    </xsl:if> 
  </xsl:template>
  
</xsl:stylesheet>


No comments: