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

RE: XML to XML

Subject: RE: XML to XML
From: m.vanrootseler@xxxxxxxxx
Date: Fri, 22 Jun 2001 13:07:05 +0200
xsl add atribute
Thanks Rob, this is what I needed! You've just saved me a lot of work, so I
owe you a pint :-)

Mick


-----Oorspronkelijk bericht-----
Van: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]Namens Rob Lugt
Verzonden: vrijdag 22 juni 2001 12:27
Aan: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Onderwerp: Re:  XML to XML


> I want to convert the following XML:
>
> <line>One two. Testing.</line>
>
> into:
>
> <line><word>One</word> <word>two</word>. <word>Testing</word>.</line>
>
> Problem number two. I want to add an atribute to the <word> tag which
gives
> the position of the word in the line: <word wordID=" ">.
>

I think the easiest way to deal with this is to group all the characters
that you don't want a word to contain into a single character using
translate().  The result of this can be used for your tokenizing, but you
still use the original text when outputing the bits between the tokens.
It's a bit long-winded and I expect others could improve it, but this
template should do the trick:-

<xsl:template match="line">
    <line>
        <xsl:call-template name="test"/>
    </line>
</xsl:template>

<xsl:template name="test">
   <xsl:param name="text" select="."/>
   <xsl:param name="count" select="1"/>
   <xsl:variable name="x" select="translate($text, '.,', '  ')"/>

      <xsl:choose>
         <xsl:when test="contains($x, ' ')">
            <xsl:variable name="word" select="substring-before($x, ' ')"/>
            <xsl:variable name="wordlen" select="string-length($word)"/>
            <xsl:if test="$wordlen">
                <word wordID="{$count}"><xsl:value-of
select="$word"/></word>
            </xsl:if>
            <xsl:value-of select="substring($text, $wordlen+1, 1)"/>
            <xsl:call-template name="test">
                <xsl:with-param name="text" select="substring($text,
$wordlen+2)"/>
                <xsl:with-param name="count"
select="$count+boolean($wordlen)"/>
            </xsl:call-template>
         </xsl:when>
         <xsl:when test="$text">
            <word wordID="{$count}">
             <xsl:value-of select="$text"/>
            </word>
         </xsl:when>
      </xsl:choose>
</xsl:template>

Hope this helps!
~Rob

--
Rob Lugt
ElCel Technology
http://www.elcel.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • XML to XML
    • m . vanrootseler - Fri, 22 Jun 2001 05:13:08 -0400 (EDT)
      • Rob Lugt - Fri, 22 Jun 2001 06:28:12 -0400 (EDT)
        • m . vanrootseler - Fri, 22 Jun 2001 07:12:30 -0400 (EDT) <=

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.