 |
| 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
|
|
|
|

July 21st, 2006, 02:31 PM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

July 21st, 2006, 03:40 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 22nd, 2006, 12:44 PM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 22nd, 2006, 01:23 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 22nd, 2006, 03:11 PM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 22nd, 2006, 05:50 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 22nd, 2006, 08:22 PM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Once again thanks for all the help!
I bought your book today....
xslt 2.0 3rd edition Programmer's reference
|
|
 |