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

Re: XPath (regex?) question about $& in an XSLT styl

Subject: Re: XPath (regex?) question about $& in an XSLT stylesheet
From: "Mark Wilson" <mark@xxxxxxxxxxxx>
Date: Mon, 18 Aug 2008 17:10:12 -0700
Re:  XPath (regex?) question about $& in an XSLT   styl
Thanks!
That was perfect.
Mark

--------------------------------------------------
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Sent: Monday, August 18, 2008 4:59 PM
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re:  XPath (regex?) question about $& in an XSLT  stylesheet

Actually, even creating the first group is unnecessary. This revision only captures a single group being the parenthesized content of the string.

I hope this helps.

. . . . . . . . . . . Ken

t:\ftemp>type wilson.xml
<Person>Benes (senior), Frances</Person>

t:\ftemp>type wilson.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:template match="*">
  <xsl:copy>
    <xsl:value-of select="replace(., '.*(\([a-zA-Z]+\)).*', '$1' )"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>xslt2 wilson.xml wilson.xsl con
<?xml version="1.0" encoding="UTF-8"?><Person>(senior)</Person>
t:\ftemp>


At 2008-08-18 19:07 -0400, I wrote:
At 2008-08-18 15:46 -0700, Mark Wilson wrote:
I have an element
<Person>Benes (senior), Frances</Person>.

I can remove the '(senior)' from the data using
<xsl:value-of select="replace(., '\([a-zA-Z]+\)', ' ' )"/>

which gives me
<Person>Benes , Frances</Person>
but I want just the opposite:
<Person>(senior)</Person>

You can use regular expression groups in XSLT using unescaped parentheses. I hope the example below helps where I wrap the text before, in, and after as three groups. Creating the third group is unnecessary, but I left it in for completeness. You might need something more creative based on what you allow in your input.


. . . . . . . . . . Ken

t:\ftemp>type wilson.xml
<Person>Benes (senior), Frances</Person>

t:\ftemp>type wilson.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:template match="*">
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:value-of select="replace(., '(.*)(\([a-zA-Z]+\))(.*)', '$2' )"/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>xslt2 wilson.xml wilson.xsl con
<?xml version="1.0" encoding="UTF-8"?><Person>(senior)</Person>
t:\ftemp>


--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

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.