Re: [xsl] empty element question

Subject: Re: [xsl] empty element question
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 16 Oct 2009 16:15:20 -0400
At 2009-10-16 13:04 -0700, M C wrote:
How do I transform this input document:

  <div>
    <p>string 1<lb/>string 2</p>
  </div>

to this output document?

  <div>
    <p>string 1</p>
    <p>string 2</p>
  </div>

This is a very limited specification, so I tried to be as general as possible in the example below. You use only text, but what if you have other formatting? I've tried to accommodate that.


And I don't see the issue as an empty element issue, but as a content grouping issue.

The principle I've used below is to create groups starting with each line-break element, and put out each group using the parent element.

I hope this helps.

. . . . . . . . . . Ken

p.s. registration is still open for public XSLT/XQuery hands-on class November 2-6, 2009


T:\ftemp>type morgan.xml <div> <p>string 1<lb/>string 2</p> </div>

T:\ftemp>xslt2 morgan.xml morgan.xsl
<?xml version="1.0" encoding="UTF-8"?><div>
    <p>string 1</p><p>string 2</p>
  </div>
T:\ftemp>type morgan.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:template match="*[lb]">
  <!--remember where we are-->
  <xsl:variable name="this" select="."/>
  <!--determine all of the groups of nodes-->
  <xsl:for-each-group select="node()" group-starting-with="lb">
    <!--return to the node being copied-->
    <xsl:for-each select="$this">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <!--but only copy that content not including the break-->
        <xsl:apply-templates select="current-group()[not(self::lb)]"/>
      </xsl:copy>
    </xsl:for-each>
  </xsl:for-each-group>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>


-- Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes in Copenhagen Denmark and Washington DC USA, October/November 2009 Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread