Re: [xsl] Response to Wolfgang Laun, Michele R Combs and G Ken Holman re Question on search and replace in XSL

Subject: Re: [xsl] Response to Wolfgang Laun, Michele R Combs and G Ken Holman re Question on search and replace in XSL
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 09 Apr 2012 14:57:23 -0400
At 2012-04-09 18:35 +0000, Peterson, Melanie S. wrote:
The input for the XML can be any number of triads of information. So for example, the input could be

aaa;bbb;ccc

The input could also be

aaa;bbb;ccc;ddd;eee;fff

Every three items needs to be formatted into a set of 3 columns.

Ah, okay ... this is new. This definitely is easier to deal with using XSLT 2.0.


So the first example needs to look like this:

<tr><td valign="top" width="180"></td><td valign="top" width="180">aaa</td><td valign="top" width="180">bbb</td><td valign="top" width="180">ccc</td></tr>

The second example needs to look like this:

<tr><td valign="top" width="180"></td><td valign="top" width="180">aaa</td><td valign="top" width="180">bbb</td><td valign="top" width="180">ccc</td></tr>
<tr><td valign="top" width="180"></td><td valign="top" width="180">ddd</td><td valign="top" width="180">eee</td><td valign="top" width="180">fff</td></tr>


There can be any number of triads of this type, one, two fifteen, I can't predict. To complicate things further, I cannot rely on there always being three pieces of information in each triad. So, I might have

aaa;;ccc

which needs to look like this:

<tr><td valign="top" width="180"></td><td valign="top" width="180">aaa</td><td valign="top" width="180"></td><td valign="top" width="180">ccc</td></tr>

Okay, then ... I've got some XSLT 2.0 code below for you based on this new requirement. But the code above doesn't mesh with the code that you show next.


As I said, based on your input, I've gotten closer. Here's what I have so far:

<tr>
    <td valign="top" width="180"></td>
    <xsl:apply-templates select="APName" />
</tr>

Right away I see a problem: the handling of APName is *inside* of a <tr>. And you cannot have in your output a <tr> as a child of a <tr>. So your code above cannot be producing <tr> elements during the handling of APName. You have to handle APName outside of the <tr> so that APName can create the <tr> elements.


I hope the example XSLT 2.0 code below helps. It is tokenizing the input string into pieces and then grouping the pieces into groups of three and creating one row for each group.

. . . . . . . . Ken


t:\ftemp>type melanie1.xml <?xml version="1.0" encoding="UTF-8"?> <tests> <APName>aaa;bbb;ccc</APName> <APName>aaa;bbb;ccc;ddd;eee;fff</APName> <APName>aaa;;ccc</APName> </tests> t:\ftemp>call xslt2 melanie1.xml melanie.xsl <?xml version="1.0" encoding="UTF-8"?>

Input value: "aaa;bbb;ccc"

Processed output:

<tr>
   <td valign="top" width="180"/>
   <td>aaa</td>
   <td>bbb</td>
   <td>ccc</td>
</tr>

Input value: "aaa;bbb;ccc;ddd;eee;fff"

Processed output:

<tr>
   <td valign="top" width="180"/>
   <td>aaa</td>
   <td>bbb</td>
   <td>ccc</td>
</tr>
<tr>
   <td valign="top" width="180"/>
   <td>ddd</td>
   <td>eee</td>
   <td>fff</td>
</tr>

Input value: "aaa;;ccc"

Processed output:

<tr>
   <td valign="top" width="180"/>
   <td>aaa</td>
   <td/>
   <td>ccc</td>
</tr>

t:\ftemp>type melanie.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="APName">
Input value: "<xsl:value-of select="."/>"

Processed output:
<!--create groups of three using the tokenized input-->
<xsl:for-each-group select="tokenize(.,';')"
                    group-by="( position() - 1 ) idiv 3">
  <!--each group produces a row-->
  <tr>
    <td valign="top" width="180"></td>
    <!--the members of the group make up the remaining columns-->
    <xsl:for-each select="current-group()">
      <td>
        <xsl:value-of select="."/>
      </td>
    </xsl:for-each>
  </tr>
</xsl:for-each-group>

</xsl:template>

</xsl:stylesheet>
t:\ftemp>echo Done!
Done!



--
Public XSLT, XSL-FO, UBL and code list classes in Europe -- May 2012
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