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

RE: Function to Extract integer from string

Subject: RE: Function to Extract integer from string
From: "Philip Vallone" <philip.vallone@xxxxxxxxxxx>
Date: Tue, 24 Oct 2006 09:21:21 -0400
regex extract numbers from string
David and Andrew,

With both of your suggestions it works perfect. BTW, what does
"'regex="\d+\.?\d*"' do?


I combined them as follows:

<xsl:for-each-group select="//Title" group-by=".">
	<xsl:if test="contains(., ''')">
		<xsl:variable name="all" select="replace(.,'[^
0-9\.]','')"/>
		<xsl:variable name="numbers" as="xs:string*">
			<xsl:analyze-string select="$all" regex="\d+\.?\d*">
				<xsl:matching-substring>
					<xsl:sequence select="."/>
					</xsl:matching-substring>
				</xsl:analyze-string>
			</xsl:variable>
			<xsl:for-each-group select="$numbers" group-by=".">
			<xsl:sort select="current-grouping-key()"/>
			<xsl:sequence select="."/>
			<p/>
		</xsl:for-each-group>
	</xsl:if>
</xsl:for-each-group>

Which produces the desired results:

21.197
21.197

21.199

91.407

121.133

Thanks, again. Now I'll figure out how to group them.

Phil

-----Original Message-----
From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx]
Sent: Tuesday, October 24, 2006 8:24 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re:  Function to Extract integer from string

On 10/24/06, Philip Vallone <philip.vallone@xxxxxxxxxxx> wrote:
> Hello Everyone,
>
> I have numerous xml documents that have content that refers to US code of
> Federal Regulations (CFRs) e.g.
>
> 'GENERAL ['21.197]' or 'PERMIT ['21.197 & '21.199]'
>
> Is there a simple string function that extracts integers from a string?
>
> I am trying to use a series of functions to do the same. e.g.
>
> <xsl:for-each-group select="//Title" group-by=".">
>         <xsl:if test="contains(., ''')">
>                 <xsl:variable name="before"
> select="substring-after(.,'[')"/>
>                 <xsl:variable name="after"
> select="substring-before($before,']')"/>
>                 <xsl:variable name="removespace" select="tokenize($after,
> '&amp;')"/>
>                 <xsl:value-of select="$removespace"/>
>                 p/>
>         </xsl:if>
> </xsl:for-each-group>
>
> Which produces:
>
> '21.197
> '21.197 '21.199
>
> Desired result:
>
> 21.197
> 21.197
> 21.199
>
> I will eventually group and sort by number.

How about:

	<xsl:variable name="numbers" as="xs:string*">
		<xsl:analyze-string select="//Title" regex="\d+\.?\d*">
			<xsl:matching-substring>
				<xsl:sequence select="."/>
			</xsl:matching-substring>
		</xsl:analyze-string>
	</xsl:variable>

	<xsl:for-each-group select="$numbers" group-by=".">
		<xsl:sort select="current-grouping-key()"/>
		<xsl:sequence select="."/>
	</xsl:for-each-group>

You might want to tight up the regex a little to not allow "2." for
example...

cheers
andrew

Current Thread

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.