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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:37:38+00:00 2026-05-28T01:37:38+00:00

I have an xml file with 000’s of lines with elements that differ only

  • 0

I have an xml file with 000’s of lines with elements that differ only by some sub-string that appears in various attributes and element content.

So i’ll like to ‘condense’ the xml and have a stylesheet create xml on the fly by substituting known sub-strings at runtime with known substitutions.
A stylesheet approach would seem to be the elegant solution to this problem given what i’ve read about them, but if it works i really don’t mind how i get a solution.

This principal would need to be generic i.e apply to xml elements with child nodes ‘n levels deep’
eg

‘condensed’ xml could look like

<element id="11[substitute here]11">
    <name>[substitute here]</name>
        <settings>
            <setting>
                <name>[substitute here]Setting</name>
                <!-- could be more elements here, n levels deep -->
            </setting>
        </settings>
    <moreConfig>zz[substitute here]zz</moreConfig>
</element>

expanded xml, substituting ‘[substitute here]’ with ‘aaa’ and then ‘bbb’, would then look like

<element id="11aaa11">
    <name>aaa</name>
        <settings>
            <setting>
                <name>aaaSetting</name>
                <!-- could more elements here, n levels deep -->
            </setting>
        </settings>
    <moreConfig>zzaaazz</moreConfig>
</element>  

    <element id="11bbb11">
        <name>bbb</name>
            <settings>
                <setting>
                    <name>bbbSetting</name>
                    <!-- could more elements here, n levels deep -->
                </setting>
            </settings>
        <calendar>zzbbbzz</calendar>
    </element>

I’m doing this in java 6, so my understanding is if using xsl only 1.0 is supported.

Hope i’ve outlined the problem clearly, appreciate any help!

many 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-05-28T01:37:39+00:00Added an answer on May 28, 2026 at 1:37 am

    This transformation:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:my="my:my">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <my:reps>
          <r>aaa</r>
          <r>bbb</r>
         </my:reps>
    
         <xsl:variable name="vReps"
              select="document('')/*/my:reps/r"/>
    
         <xsl:template match="node()|@*">
           <xsl:param name="pCurrentRep"/>
             <xsl:copy>
               <xsl:apply-templates select="node()|@*">
                <xsl:with-param name="pCurrentRep" select="$pCurrentRep"/>
               </xsl:apply-templates>
             </xsl:copy>
         </xsl:template>
    
         <xsl:template match="config">
          <xsl:param name="pCurrentRep"/>
    
          <xsl:variable name="vDoc" select="."/>
          <xsl:copy>
           <xsl:for-each select="$vReps">
            <xsl:apply-templates select="$vDoc/*">
             <xsl:with-param name="pCurrentRep" select="."/>
            </xsl:apply-templates>
           </xsl:for-each>
          </xsl:copy>
         </xsl:template>
    
         <xsl:template match="@*[contains(., '[substitute here]')]"
          priority="2">
          <xsl:param name="pCurrentRep"/>
    
          <xsl:attribute name="{name()}">
           <xsl:call-template name="replace">
            <xsl:with-param name="pRep" select="$pCurrentRep"/>
           </xsl:call-template>
          </xsl:attribute>
         </xsl:template>
    
         <xsl:template match="text()[contains(., '[substitute here]')]"
         priority="2">
          <xsl:param name="pCurrentRep"/>
    
           <xsl:call-template name="replace">
            <xsl:with-param name="pRep" select="$pCurrentRep"/>
           </xsl:call-template>
         </xsl:template>
    
         <xsl:template name="replace">
          <xsl:param name="pText" select="."/>
          <xsl:param name="pTarget" select="'[substitute here]'"/>
          <xsl:param name="pRep"/>
    
          <xsl:if test="string-length($pText) >0">
            <xsl:choose>
              <xsl:when test="not(contains($pText, $pTarget))">
                <xsl:value-of select="$pText"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="substring-before($pText, $pTarget)"/>
                <xsl:value-of select="$pRep"/>
    
                <xsl:call-template name="replace">
                  <xsl:with-param name="pText" select=
                     "substring-after($pText, $pTarget)"/>
                  <xsl:with-param name="pTarget" select="$pTarget"/>
                  <xsl:with-param name="pRep" select="$pRep"/>
                </xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
         </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <config>
        <element id="11[substitute here]11">
            <name>[substitute here]</name>
            <settings>
                <setting>
                    <name>[substitute here]Setting</name>
                    <!-- could be more elements here, n levels deep -->
                </setting>
            </settings>
            <moreConfig>zz[substitute here]zz</moreConfig>
        </element>
    </config>
    

    produces the wanted, correct result:

    <config>
       <element id="11aaa11">
          <name>aaa</name>
          <settings>
             <setting>
                <name>aaaSetting</name><!-- could be more elements here, n levels deep -->
             </setting>
          </settings>
          <moreConfig>zzaaazz</moreConfig>
       </element>
       <element id="11bbb11">
          <name>bbb</name>
          <settings>
             <setting>
                <name>bbbSetting</name><!-- could be more elements here, n levels deep -->
             </setting>
          </settings>
          <moreConfig>zzbbbzz</moreConfig>
       </element>
    </config>
    
    • 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 that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have a very large XML file with 40,000 tag elements. When i am
I have an xml file providing data for a datagrid in Flex 2 that
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have a XML File like that <?xml version=1.0 encoding=utf-8 ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName>
I have a collection of about 8,000 test scores in an XML file. Using
I have an android project with strings.xml file, what contains string values. Today when
I have a big xml database (30 000 files, 1.3 Go). One file in
I have the following SyntaxDefinition.xml file that I am using to create syntax highlighting
I have xml-file. I need to read it, make some changes and write new

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.