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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:56:39+00:00 2026-06-17T06:56:39+00:00

I have the following structure of xml data to transform: <root> <main1> <text-body> <title>A</title>

  • 0

I have the following structure of xml data to transform:

 <root>
    <main1>
            <text-body>
                <title>A</title>
                <subtitle>A</subtitle>
            </text-body>
    <!-- other stuff -->
            <text-body>
                <titel>Aa</titel>
                <subtitel>Aa</subtitel>
            </text-body>
    <!-- other stuff -->
            <text-body>
                <titel>Aaa</titel>
                <subtitel>Aaa</subtitel>
            </text-body>
    </main1>
    <main2>
        <text-body>
            <title>B</title>
            <subtitle>B</subtitle>
            <body>B</body>
        </text-body>
        <text-body>
            <title>C</title>
            <subtitle>C</subtitle>
            <body>C</body>
        </text-body>
        <text-body>
            <title>D</title>
            <subtitle>D</subtitle>
            <body>D</body>
        </text-body>
    </main2>
    </root>

And I need to replace the data in main/text-body with the data in main2/text-body, but keep the other stuff in main1. The output should look like this:

<root>
        <main1>
              <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
                </text-body>
        <!-- other stuff -->
              <text-body>
                 <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
              </text-body>
        <!-- other stuff -->
              <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
              </text-body>
        </main1>
        <main2>
            <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
            <text-body>
                <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
            </text-body>
            <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
            </text-body>
        </main2>
        </root>

I have the following xsl-code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml"/>

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

    <xsl:template match="main1/text-body">
           <xsl:param name="count" select="count(preceding-sibling::node())"/>
           <xsl:copy-of select="/root/main2/text-body[$count]"/>
    </xsl:template>

</xsl:stylesheet>

I try to get the current node index of main1/text-body to fill in the right text-body of main2. But it doesn´t work. Here is the current output:

<?xml version="1.0" encoding="UTF-8"?><root>
        <main1>
              <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
        <!-- other stuff -->

        <!-- other stuff -->

        </main1>
        <main2>
            <text-body>
                <title>B</title>
                <subtitle>B</subtitle>
                <body>B</body>
            </text-body>
            <text-body>
                <title>C</title>
                <subtitle>C</subtitle>
                <body>C</body>
            </text-body>
            <text-body>
                <title>D</title>
                <subtitle>D</subtitle>
                <body>D</body>
            </text-body>
        </main2>
        </root>

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-17T06:56:41+00:00Added an answer on June 17, 2026 at 6:56 am

    XPath and XSLT are 1-based indexes. In order to select the first item, your predicate filter would look for [1], not [0].

    Given that, the variable $count needs to have 1 added to the count() of the preceding-sibling selection. Also, you should be counting the text-body elements that are preceding-siblings, not all/any node()

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="main1/text-body">
            <xsl:param name="count" select="count(preceding-sibling::text-body)+1"/>
            <xsl:copy-of select="/root/main2/text-body[$count]"/>
        </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 the following structure of xml data to transform: <root> <main1> <page> <text-body>
I have the following structure of xml data to transform: <chapter> <title>Text</title> <subtitle>Text</subtitle> <paragraph>Text</paragraph>
I have an XML file of the following structure <Root> <Child Name=First> <Data Name=a
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
Lets say I have the following xml. <root> <data> <a>ATTITUDE_ANNOYED</a> <b>ATTITUDE_CAUTIOUS</b> <c>25</c> <d>30</d> </data>
I have an XML file with the following structure: <root> <subroot id=someID> <val1 value=a/>
I have the following XML structure: <example> <data> <numberGer>3,40</numberGer> </data> <data> <numberGer>7,40</numberGer> </data> <data>
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following XML data. I have no control on the structure of
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data

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.