xsl-list
[Top] [All Lists]

RE: [xsl] Value of the variable

2009-11-27 02:41:27
At 2009-11-26 18:02 +0530, I wrote:
Interestingly, this question was asked about an hour ago on LinkedIn. The poster of that question gave an explicit input and an explicit output, so it was very easy to be able to answer.

For those following this thread on XSL-List, the LinkedIn parallel thread with the same problem was augmented with new requirements not included in the original post. Apparently, the commas are the overarching determinants in the grouping of the information ... not commas and elements as guessed at for the first time around.

This changes the problem from a simple iteration into a more elaborate grouping. A complete solution is below for readers of this archive who may have a similar need of working with portions of text nodes.

. . . . . . . . Ken

t:\ftemp>type siddhi.xml
<book>key1, k<i>e</i>y2, key<b>3</b>, <u>ke</u>y4, <b>k</b>ey<i>5</i></book>

t:\ftemp>xslt2 siddhi.xml siddhi.xsl
<?xml version="1.0" encoding="UTF-8"?><book><key>key1</key>, <key>k<i>e</i>y2</key>, <key>key<b>3</b></key>, <key><u>ke</u>y4</key>, <key><b>k</b>ey<i>5</i></key></book>
t:\ftemp>type siddhi.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="book">
  <!--preserve the book element and any of its attributes-->
  <xsl:copy>
    <xsl:copy-of select="@*"/>

    <!--determine all of the portions that make up the line-->
    <xsl:variable name="portions" as="element(portion)*">
      <xsl:apply-templates mode="make-portions"/>
    </xsl:variable>

    <!--those portions ending with a comma make up the new results-->
    <xsl:for-each-group select="$portions"
                        group-ending-with="*[(_at_)comma-end='true']">
      <!--surround each group of portions-->
      <key>
        <xsl:copy-of select="current-group()/node()"/>
      </key>
      <xsl:if test="position()!=last()">, </xsl:if>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<!--make numerous portions from a given text node-->
<xsl:template match="text()" mode="make-portions">
  <xsl:for-each select="tokenize(.,',\s+')">
    <portion comma-end="{position()!=last()}">
      <xsl:value-of select="."/>
    </portion>
  </xsl:for-each>
</xsl:template>

<!--make a single portion from a given element node-->
<xsl:template match="*" mode="make-portions">
  <portion><xsl:copy-of select="."/></portion>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>



--
Vote for your XML training:   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(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--