[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Value of the variable

Subject: RE: Value of the variable
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 27 Nov 2009 13:10:46 +0530
RE:  Value of the variable
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="*[@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@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.