Solved

How to remove values and parentheses from a string?

Posted on 2007-04-05
4
227 Views
Last Modified: 2012-06-22
Greetings,

How can I only return the string value that is not included within parentheses?  For example, if I have an element <LINK TTL="17" S="210.4-01(a)(3)(i)(A)"> and I would only like the @S attribute to only return "210.4-01", how can this be processed?

Any advice or guidance is greatly appreciated!

Best regards,
Jamie
0
Comment
Question by:jmc430
  • 2
4 Comments
 
LVL 10

Expert Comment

by:BobSiemens
ID: 18862350
<xsl:template name="DumpNonParenData">
  <xsl:param name="TheString" />
  <xsl:choose>
    <xsl:when test="contains($TheString, '(')">



0
 
LVL 10

Accepted Solution

by:
BobSiemens earned 400 total points
ID: 18862370
Oops

This is a quick, typed from the top of my head, method.  

Basically, you recursively call the function.  It will dump the whole string if there are no parens.  If there are parens, it dumpt the pre-paren part and calls itself with the post-paren part.

<xsl:template name="DumpNonParenData">
  <xsl:param name="TheString" />
  <xsl:choose>
    <xsl:when test="contains($TheString, '(')">
      <xsl:value-of select="substring-before($TheString, '(')"/>
      <xsl:call-template name="DumpNonParenData">
        <xsl:with-param name="TheString" >
          <xsl:value-of select="substring-after($TheString, ')')"/>
        </xsl:with-param>
      </xsl:call-template>      
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$TheString"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
 
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 100 total points
ID: 18862656
If you are using XSLT1 you need a recursive method as Bob suggested

This example is a perfect usecase for XSLT2 though.
(If I had to do some similar things like this in a project, I would use XSLT2)
So if by any chance you can use Saxon8B in this project,
I recommend you do
Here is the XSLT2 solution, look how easy with regular expressions

    <xsl:template match="LINK">
        <xsl:value-of select="replace(@S, '\([^\)]+\)', '')" />
    </xsl:template>

cheers

Geert
0
 

Author Comment

by:jmc430
ID: 18864805
Hi Geert,

Thanks for your helpful response!
I cannot use Saxon8B, but I appreciate your comments!

Hi BobSiemens,

Thanks for your solution.  It works!

Thanks again and happy holidays! :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML namaspace 2 44
XML filtering Windows Event Viewer 10 48
Word files to Wordpress Posts 2 43
Insert Powershell variable into XML 4 36
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now