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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:53:01+00:00 2026-05-16T14:53:01+00:00

This question is an extension of this one . I have many values, as

  • 0

This question is an extension of this one. I have many values, as seen below:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>foo bar</elem2>
    <elem3>foo bar foo</elem3>
    <elem4>foofoo</elem4>
</myStuff>

I’ve been doing a copy-of over MOST the elems (the select in the copy-of is very specific) and then doing a find-and-replace on the resulting XML, but I’d like to combine the two. In most of the situations where I’ve applied this, I would replace anything that said <xsl:value-of select="whatever"> with <xsl:apply-templates select="whatever"> and use the below template:

<xsl:template match="*">
    <xsl:variable name="rep1">
        <xsl:choose>
            <xsl:when test='matches(., ".*foo.*")'>
                <xsl:value-of select='replace(., "foo", "qwerty")' />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select='./text()' />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="rep2">
        <xsl:choose>
            <xsl:when test='matches(., ".*bar.*")'>
                <xsl:value-of select='replace($rep1, "bar", "myBar")' />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select='$rep1' />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$rep2" />
</xsl:template>

I would like to use a similar template to replace the copy-of in my code (because I’m making the same replacements as in my other files, so I could use the same template), but I’m unsure of how to do this.

The output would look like this:

<myOtherStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myOtherStuff>

All help is appreciated and thanks in advance!

  • 1 1 Answer
  • 2 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-16T14:53:01+00:00Added an answer on May 16, 2026 at 2:53 pm

    With another approach, this stylesheet:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes"/>
        <xsl:variable name="vReps" as="element()*">
            <rep target="foo" newval="qwerty"/>
            <rep target="bar" newval="myBar"/>
        </xsl:variable>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="text()">
            <xsl:analyze-string select="." 
             regex="{string-join($vReps/concat('(',@target,')'),'|')}">
                <xsl:matching-substring>
                    <xsl:value-of select="$vReps[@target=current()]/@newval"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <myStuff>
        <elem1>asdf</elem1>
        <elem2>qwerty myBar</elem2>
        <elem3>qwerty myBar qwerty</elem3>
        <elem4>qwertyqwerty</elem4>
    </myStuff>
    

    Also, this input (from comments):

    <myStuff>
        <elem>asdfooasdf</elem>
    </myStuff>
    

    Output:

    <myStuff>
        <elem>asdqwertyasdf</elem>
    </myStuff>
    

    Note: This RegExp union perform the replacement all at once. This may differ of sequencely fn:replace calls: suppose this replacement targets “A” -> “B” and “B” -> “C” on this string “AB”, union replacement should output “BC” and sequencely calls output “CC”.

    Note 2: With regard to matching order, do note that RegExp union follows its own rules (see specs, more specific If two alternatives within the supplied $pattern both match at the same position in the $input string, then the match that is chosen is the first.) and sequencely fn:replace calls follows strictly user defined order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an extension to the one raised here: Using factory_girl in Rails
This is an extension of this question: SQLite problem selecting two columns as one
My question is an extension of that one: Immutable numpy array? This code prints
This question has been brought up before, and I have searched many of the
I have a problem very similar to the one mentioned in this question .
I have asked this question in Radiant CMS google group, but no one answered
this question is an extension of the question here i tried to extend Dane's
This question is an extension of this Related question . Taking Derick's advice, I
This question already has an answer here: What are Extension Methods? 5 answers Usage
As an extension to this question here Linking JavaScript Libraries in User Controls I

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.