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 896691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:46:06+00:00 2026-05-15T14:46:06+00:00

I’ve simplified the problem somewhat, but I hope I’ve still captured the essence of

  • 0

I’ve simplified the problem somewhat, but I hope I’ve still captured the essence of my problem.

Let’s say I have the following simple XML file:

<main>
  outside1
  ===BEGIN===
    inside1
  ====END====
  outside2
  =BEGIN=
    inside2
  ==END==
  outside3
</main>

Then I can use the following the XSLT 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

  <xsl:analyze-string select="." regex="=+BEGIN=+">
     <xsl:matching-substring>
        <section/>
     </xsl:matching-substring>
     <xsl:non-matching-substring>
          <xsl:analyze-string select="." regex="=+END=+">  
             <xsl:matching-substring>
                <_section/>
             </xsl:matching-substring>
             <xsl:non-matching-substring>
                <xsl:value-of select="."/>
             </xsl:non-matching-substring>
          </xsl:analyze-string>
     </xsl:non-matching-substring>
  </xsl:analyze-string>

</xsl:template>

</xsl:stylesheet>

To transform it to the following:

<?xml version="1.0" encoding="UTF-8"?>
  outside1
  <section/>
    inside1
  <_section/>
  outside2
  <section/>
    inside2
  <_section/>
  outside3

Here are the questions:

Multiple regexes

Is there a better way to match two different regexes rather than nesting them inside another like what was done above?

  • What if they’re not easily nestable like this?
  • Can I have XSL templates to match and transform regex matches in a text()?
    • In this case, I’d have two templates, one for each regex
    • If possible, this would be the ideal solution

Opening and closing elements on regex matches

Obviously, instead of:

<section/>
   inside
<_section/>

What I really want eventually is:

<section>
   inside
</section>

So how would you do this? I’m not sure if it’s even possible to open an element in one regex match and close it in another (i.e. What if there is no match for the closer? The result will not be well-formed XML!), but it seems like this task is quite typical that there has to be an idiomatic solution for them.

Note: we can assume that sections will not overlap, and thus also will not nest. We can also assume that they will always appear in proper pairs.


Additional info

So essentially I’m trying to accomplish what in Perl would succintly be something like:

s/=+BEGIN=+/<section>/
s/=+END=+/<\/section>/

I’m looking for a way to do this in XSLT instead, because:

  • It’d be more robust with regards to the context of the regex match
    • (i.e. it should only transform text() nodes)
  • It’d also be more robust with regards to matching various XML entities
  • 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-05-15T14:46:07+00:00Added an answer on May 15, 2026 at 2:46 pm

    This transformation:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     exclude-result-prefixes="xs"
    >
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="text()">
       <xsl:analyze-string select="." flags="mx"
        regex="=+BEGIN=+((.|\n)*?)=+END=+">
    
       <xsl:matching-substring>
        <section><xsl:value-of select="regex-group(1)"/></section>
       </xsl:matching-substring>
    
       <xsl:non-matching-substring>
        <xsl:value-of select="."/>
       </xsl:non-matching-substring>
     </xsl:analyze-string>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <main>
      outside1
      ===BEGIN===
        inside1
      ====END====
      outside2
      =BEGIN=
        inside2
      ==END==
      outside3
    </main>
    

    produces the wanted result:

    <main>
      outside1
      <section>
        inside1
      </section>
      outside2
      <section>
        inside2
      </section>
      outside3
    </main>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.