[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Text retrieval problem
<big-picture> The XSLT + Java way described below could be used for "Match" part of 'regular expressions' Implementing "Split" part of reguilar expressions will turn XSLT into something different than it is now. Is there any interest to get 'Split' mapped into XSLT ? I can do that. ( I also think Matt Sergeant can do that maybe better than me ;-) </big-picture> ----- Original Message ----- From: Matthew Bentley > Hi there - > I have an element <act> which might contain the text "[1981] 2 NZLR 132" > Or it could contain: 2 NZLR 132 > Or it could contain: [1981] 2 132 > > Essentially I want to grab the last number in the text sequence, the "132". > How can I do this? In many ways. Way 0. 'make your atoms small' ( I mean split your XML data to as small possible chunks as you can. ( Do not store, for example "Version 1.0" but only "1.0" e t.c. )) This is just some generall suggestion. I have tested the stylesheets with file: <doc> <act>[1981] 2 132</act> <act>2 NZLR 500000</act> </doc> It produces: 132 500000 Way 1. Plain XSLT. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/doc"> <xsl:apply-templates select="act"/> </xsl:template> <xsl:template match="act"> <xsl:call-template name="getLastWord"> <xsl:with-param select="text()" name="s"/> </xsl:call-template> <xsl:text> </xsl:text> </xsl:template> <xsl:template name="getLastWord"><xsl:param name="s"/> <xsl:variable select="substring-after($s, ' ')" name="tail"/> <xsl:choose><xsl:when test="contains($tail, ' ')"> <xsl:call-template name="getLastWord"> <xsl:with-param select="$tail" name="s"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$tail"/></xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> Way 2. XSLT + Java. ( Cause it is *really* task for java layer, not for XSLT layer. ) public class StringHelper { public static String getLastWord( String s ) { return ( s.lastIndexOf(' ') >= 0 ) ? s.substring( s.lastIndexOf(' ') + 1 ): ""; } } <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:str="http://www.jclark.com/xt/java/StringHelper" > <xsl:template match="/doc"> <xsl:apply-templates select="act"/> </xsl:template> <xsl:template match="act"> <xsl:value-of select=" str:getLastWord( string(text()) )"/> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> Way 3. Honestly - I have not wrote the stylesheets above. I wrote them in XSLScript, debugged them XSLScript and then just dumped them out . Here is the XSLScript source: Plain XSLT: X:stylesheet { X:template= "/doc" { <% !! "act" %> } X:template = "act" { <% !getLastWord( s="text()" ) %> <% X:text { } %> } X:template getLastWord( s ) { <% X:var tail = "substring-after($s, ' ')" %> <% X:if "contains($tail, ' ')" { <% !getLastWord( s="$tail" ) %> } else { <% {$tail} %> } %> } } XSLT + Java: X:stylesheet, str="StringHelper" { X:template = "/doc" { <% !! "act" %> } X:template = "act" { <% { str:getLastWord( string(text()) ) } %> <% X:text { } %> } } Rgds.Paul. XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|