RE: [xsl] Value of the variable

Subject: RE: [xsl] Value of the variable
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Fri, 27 Nov 2009 11:13:14 +0100
> 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

I am not sure which processors support similar, but DataPower
XSLT processor provides (proprietary) extension functions
dp:parse() ("Parses a well-formed XML document to produce a
node set") and its reverse dp:serialize() ("Sends the output
for a specified node set as a byte stream").
These allow for a really short implementation for this problem:

$ curl --data-binary @siddi.xml http://dp1-l3.boeblingen.de.ibm.com:2051 ;
echo ; echo
<?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>

[stammw@br8ggx73 parse]$ cat p.xsl
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:dp="http://www.datapower.com/extensions";
    xmlns:str="http://exslt.org/strings";
    extension-element-prefixes="dp"
    exclude-result-prefixes="str"
    version="1.0">

  <xsl:template match="/book">
    <xsl:variable name="ser">
      <dp:serialize select="node()" omit-xml-decl="yes"/>
    </xsl:variable>
    <book>
      <xsl:for-each select="str:tokenize($ser, ',')">
        <xsl:copy-of select=
          "dp:parse(concat('&lt;key>',text(),'&lt;/key>'))"/>
        <xsl:if test="position()!=last()">, </xsl:if>
    </xsl:for-each>
    </book>
  </xsl:template>
</xsl:stylesheet>
$


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


                                                                           
             "G. Ken Holman"                                               
             <gkholman@CraneSo                                             
             ftwrights.com>                                             To 
                                       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>   
             11/27/2009 08:40                                           cc 
             AM                                                            
                                                                   Subject 
                                       RE: [xsl] Value of the variable     
             Please respond to                                             
             xsl-list@xxxxxxxx                                             
              lberrytech.com                                               
                                                                           
                                                                           
                                                                           




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