Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:06:03+00:00 2026-06-01T09:06:03+00:00

I have an xml file in which I have to find patterns and put

  • 0

I have an xml file in which I have to find patterns and put a ” marks around the matching specific pattern.

<xml>
<foo>
    <id>1</id>
    <description>This is section 1, this is section 1 or 2, this is section 1 and 2</description>
</foo>
</xml>

Now in file I have to find a pattern “section 1” , “section 1 or 2” and “section 1 and 2″ and put a ” marks around the matching word.

I have written an xslt which is like this:

  <xsl:template match="text()">

<xsl:analyze-string select="." regex="section\s\d+|section\s\d+\s+or\s+\d|section\s\d+\s+and\s+\d">
  <xsl:matching-substring>
    <xsl:if test="matches(.,'section\s\d+')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
      <xsl:text>"</xsl:text>       
    </xsl:if>
    <xsl:if test="matches(.,'section\s\d+\s+or\s+\d')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
        <xsl:text>"</xsl:text>       
    </xsl:if>
    <xsl:if test="matches(.,'section\s\d+\s+and\s+\d')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
      <xsl:text>"</xsl:text>       
    </xsl:if>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="current()"/>
  </xsl:non-matching-substring>
</xsl:analyze-string>
 </xsl:template>

The problem I am facing here is that pattern “section 1” matches all the other pattern as well so I am not getting a desire result

The above transformation result is

<xml>
  <foo>
      <id>1</id>
      <description>This is "section 1", this is "section 1" or 2, this is "section 1" and 2</description>
  </foo>
</xml>

where as i want this output.

<xml>
  <foo>
    <id>1</id>
    <description>This is "section 1", this is "section 1 or 2", this is "section 1 and 2"</description>
   </foo>
</xml>

Any ideas how to implement it… and get the desire result.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:06:05+00:00Added an answer on June 1, 2026 at 9:06 am

    this appears to work on your input:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
     <xsl:template match="*">
      <xsl:copy>
       <xsl:apply-templates/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="text()">
      <xsl:analyze-string select="." regex="section\s+\d+(\s+(or|and)\s+\d+)?">
       <xsl:matching-substring>
         <xsl:text>"</xsl:text>
         <xsl:value-of select="."/>     
         <xsl:text>"</xsl:text>       
       </xsl:matching-substring>
       <xsl:non-matching-substring>
        <xsl:value-of select="current()"/>
       </xsl:non-matching-substring>
      </xsl:analyze-string>
     </xsl:template>
    
    </xsl:stylesheet>
    

    Although if you are just generating a string not elements, xsl:anayze-string is more than you need and this produces the same result:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
     <xsl:template match="*">
      <xsl:copy>
       <xsl:apply-templates/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="text()">
      <xsl:value-of  select="replace(.,'section\s+\d+(\s+(or|and)\s+\d+)?','&quot;$0&quot;')"/>
     </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an .xml file which contains this: <a:xfrm> <a:off x=381000 y=152400/> <a:ext cx=2124075
I have an xml file which I imported in my html page like this:
I have an XML file, part of which looks like this: <wave waveID=1> <well
I have an XML file which I need to parse using PHP and send
I have an XML file which XML parser choke on. A part of it
I have an XML file which has many section like the one below: <Operations>
I have an XML file which I'd like to parse into a non-XML (text)
Good morning. I have an XML file which contains lists of warning and errors
Im using c# .net , windows form application. I have a XML file which
We have a PageRoles xml file which contains the page path and the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.