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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:37:51+00:00 2026-06-18T01:37:51+00:00

Given the following xml file with the knowledge that the structure and contents can

  • 0

Given the following xml file with the knowledge that the structure and contents can change:

<something>
  <parent>
    <child>Bird is the word 1.</child>
    <child>Curd is the word 2.</child>
    <child>Nerd is the word 3.</child>
  </parent>
  <parent>
    <child>Bird is the word 4.</child>
    <child>Word is the word 5.</child>
    <child>Bird is the word 6.</child>
  </parent>
</something>

I would like a way to use xquery (and even xslt) to replace all instances of a supplied string with another. For example, replace the word “Bird” with “Dog”. Therefore the results would be:

<something>
  <parent>
    <child>Dog is the word 1.</child>
    <child>Curd is the word 2.</child>
    <child>Nerd is the word 3.</child>
  </parent>
  <parent>
    <child>Dog is the word 4.</child>
    <child>Word is the word 5.</child>
    <child>Dog is the word 6.</child>
  </parent>
</something>

I have no idea if this is even possible. Every attempt I have made has eliminated the tags. I have even tried this example (http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx), but it is for text not an entire document.

Please help!

UPDATE

I tried running with the xslt 2.0 suggestion as it seemed to fit the best. While attempting to modify it for my case, I keep coming up dry.

I want to pass in an xml parameter to define the replacements. So, modifying the xslt like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:param name="list">
<words>
  <word>
        <search>Bird</search>
    <replace>Dog</replace>
  </word>
      <word>
        <search>word</search>
    <replace>man</replace>
  </word>
</words>
  </xsl:param>


<xsl:template match="@*|*|comment()|processing-instruction()">
  <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()">
  <xsl:param name="chosen" select="." />
<xsl:for-each select="$list//word">
  <xsl:variable name="search"><xsl:value-of select="search" /></xsl:variable>
  <xsl:analyze-string select="$chosen" regex="{$search}">
    <xsl:matching-substring><xsl:value-of select="replace" /></xsl:matching-substring>
    <xsl:non-matching-substring><xsl:value-of select="$chosen"/></xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

The results are:

<something>
  <parent>
    <child>Bird is the word 1.Bird is the word 1.</child>
    <child>Curd is the word 2.Curd is the word 2.</child>
    <child>Nerd is the word 3.Nerd is the word 3.</child>
  </parent>
  <parent>
    <child>Bird is the word 4.Bird is the word 4.</child>
    <child>Word is the word 5.Word is the word 5.</child>
    <child>Bird is the word 6.Bird is the word 6.</child>
  </parent>
</something>

Needless to say, but, I don’t want it duplicated and also incorrect.

Please Help!

  • 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-18T01:37:52+00:00Added an answer on June 18, 2026 at 1:37 am

    If both XQuery and XSLT are an option, you’re probably using an XSLT 2.0 processor. If so, this should work:

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:param name="search" select="'Bird'"/>
        <xsl:param name="replace" select="'Dog'"/>
    
        <xsl:template match="@*|*|comment()|processing-instruction()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="text()">
            <xsl:analyze-string select="." regex="{$search}">
                <xsl:matching-substring><xsl:value-of select="$replace"/></xsl:matching-substring>
                <xsl:non-matching-substring><xsl:value-of select="."/></xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Using the XML input from the question, this XSLT produces the following output:

    <something>
       <parent>
          <child>Dog is the word 1.</child>
          <child>Curd is the word 2.</child>
          <child>Nerd is the word 3.</child>
       </parent>
       <parent>
          <child>Dog is the word 4.</child>
          <child>Word is the word 5.</child>
          <child>Dog is the word 6.</child>
       </parent>
    </something>
    

    Note: No elements/attributes/comments/processing-instructions would be altered in the creation of the output.


    EDIT

    The reason you’re getting duplicates is because your xsl:for-each is looping over the two word elements. If you had 3, it would output the text 3 times.

    You just need to build the regex a little differently:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
        <xsl:param name="list">
            <words>
                <word>
                    <search>Bird</search>
                    <replace>Dog</replace>
                </word>
                <word>
                    <search>word</search>
                    <replace>man</replace>
                </word>
            </words>
        </xsl:param>
    
        <xsl:template match="@*|*|comment()|processing-instruction()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="text()">
            <xsl:variable name="search" select="concat('(',string-join($list/words/word/search,'|'),')')"/>
            <xsl:analyze-string select="." regex="{$search}">
                <xsl:matching-substring>
                    <xsl:value-of select="$list/words/word[search=current()]/replace"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    </xsl:stylesheet>
    

    This will produce:

    <something>
       <parent>
          <child>Dog is the man 1.</child>
          <child>Curd is the man 2.</child>
          <child>Nerd is the man 3.</child>
       </parent>
       <parent>
          <child>Dog is the man 4.</child>
          <child>Word is the man 5.</child>
          <child>Dog is the man 6.</child>
       </parent>
    </something>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following: >>> from lxml import etree >>> contents=open('file.xml').read() >>> node=etree.fromstring(contents) How would
I have written the following piece of code that reads through a given xml-file
Can some one explain and correct the following XAML given the XML File to
Given an input xml file with following structure: <root> <record row=1 col=1 val=1 />
Given the following xml: <!-- file.xml --> <video> <original_spoken_locale>en-US</original_spoken_locale> <another_tag>somevalue</another_tag> </video> What would be
Given the following XML structure: <root> <data>x</data> <details> <some_other_element>...</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>
Given the following repository URL from my pom.xml how can I determine what the
Given the following XML file, what do the DOCTYPE , ENTITY , SYSTEM ,
Given the following XML file: <packages> <package name=Library> <distributionPoints> <distributionPoint path=http://SERVER001/SMS_DP_SMSPKGD$/CEN0003B/ /> <distributionPoint path=http://SERVER002/SMS_DP_SMSPKGD$/CEN0003B/
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <application name=foo> <movie name=tc english=tce.swf chinese=tcc.swf

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.