This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]