Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2006, 02:31 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt - need help with the text

Hi all,
this is kind of writing xslt's for the first time on my job.
here's my sample xml:
<root>
<record>
<level>Levels: L6, L7, L8, L9, L10, L11, L12.</level>
</record>
<record>
<level>Levels: L1, L2, L3, L4.</level>
</record>
</root>
I need to get just the last number which is 12 and 4 from level nodes and ignore the rest of the text.I tried using all string functions but of no use I cannot arrive @ the desired output.

All your help is appreciated.

Thanks a ton!

 
Old July 21st, 2006, 03:40 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You presumably want an XSLT 1.0 solution? Text manipulation is much easier in 2.0.

You need a recursive template with the logic: if the supplied string contains a ",", call yourself recursively with substring-after($input, ',') as the parameter. Otherwise use translate($in, '. L', '') to get rid of unwanted characters, and you're left with the required number.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2006, 12:44 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply!

but it doesn't seem to work....MAY BE I'M MISSING SOMETHING FROM U R REPLY
if I first say translate L into " translate(current(),'L','')

I will have <level> 6, 7, 8, 9, 10, 11, 12.</level>
<level>1, 2, 3, 4.</level>

If I try to use substring-after(substring-after(current(),','),',') recursively how will
I know how many substring-after to use as I 'm having 10,000 records and each level has different lengths ex: one node has <level>L1, L2</level>

and other level nodes as above.

Please help me with fixing this.

 
Old July 22nd, 2006, 01:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sounds like you haven't come across recursive programming before, at least in XSLT. Let me recommend my book...

Meanwhile, perhaps this code will make things clearer. You can run it against any source document.

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


<xsl:template match="/">
<out>
      <xsl:call-template name="lasttoken">
        <xsl:with-param name="string" select="'L1, L2, L3, L4'" />
      </xsl:call-template>
</out>
</xsl:template>

<xsl:template name="lasttoken">
    <xsl:param name="string"/>
  <xsl:choose>
    <xsl:when test="contains($string, ',')">
      <xsl:call-template name="lasttoken">
        <xsl:with-param name="string" select="substring-after($string, ',')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="translate($string, 'L. ', '')"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2006, 03:11 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you! I think I have to get your book asap....

Just one last question I'm converting XML1 TO XML2 via XSLT.....

hence the & in XSL1 has to remain & or shud be transformed to and in XML2...

how can I do this?

If I use translate(&,and) it gets a only since & is &
so translate works character by character....

this may be a very basic ques but I don't get it

 
Old July 22nd, 2006, 05:50 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Perhaps you wrote some of the & signs as &_amp; (no underscore) and they got messed up by the mailer?

Replacing strings is a common requirement and in XSLT 2.0 there is a replace() function, but in 1.0 it's done with a recursive template. It's the same principle: if the input string contains X, output

concat(substring-before($in, X), Y, substring-after($in, X))

and then call yourself to do it again in case there are any more occurrences of X.

You can find standard reusable templates for this kind of operation at http://www.exslt.org/

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2006, 08:22 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Once again thanks for all the help!

I bought your book today....

xslt 2.0 3rd edition Programmer's reference






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using XSLT to generate PDF with Japanese text XSLTUser XSLT 5 December 20th, 2007 09:01 PM
xslt adding text blitzer XSLT 3 May 19th, 2007 10:10 PM
can we identify the text in the XSLT? rajatake XSLT 7 March 22nd, 2007 08:28 AM
Retrieve hidden text box value through xslt? Hannibal XSLT 1 January 29th, 2007 05:09 AM
Text Replace with XSLT pendyalap XSLT 13 October 1st, 2006 05:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.