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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:52:04+00:00 2026-06-17T13:52:04+00:00

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

  • 0

I have the following structure of xml data to transform:

<root>
        <main1>
            <page>
                <text-body>
                    <title>K1</title>
                    <subtitle>Text</subtitle>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>K2</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>K3</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
        </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 main1/text-body with titles K1, K2, K3 with the data in main2/text-body, but keep the other text-body elements in main1. The output should look like this:

<root>
        <main1>
            <page>
                <text-body>
                    <title>B</title>
                    <subtitle>B</subtitle>
                    <body>B</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>C</title>
                    <subtitle>C</subtitle>
                    <body>C</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
            <page>
                <text-body>
                    <title>D</title>
                    <subtitle>D</subtitle>
                    <body>D</body>
                </text-body>
                <text-body>
                    <title>Text</title>
                    <subtitel>Text</subtitel>
                </text-body>
            </page>
        </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/page/text-body">
            <xsl:param name="count" select="title/substring(.,2,1)"/>
            <xsl:if test="title/substring(.,1,1)='K'">
               <xsl:copy-of select="/root/main2/text-body[$count]"/>
            </xsl:if>
        </xsl:template>

    </xsl:stylesheet>

I try to select the number in the title and to check, if there is a K in the text. But it doesn´t work. And I have no idea how to keep the other text-body elements. Here is the current output:

 <main1>
            <page>
                <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>


            </page>
            <page>
                <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>

            </page>
            <page>
                <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>

            </page>
        </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-17T13:52:05+00:00Added an answer on June 17, 2026 at 1:52 pm

    The main problem is that your count parameter is set to be a string, but if you want to use in as an index, it should be a number, so you will need to do this

    <xsl:copy-of select="/root/main2/text-body[number($count)]"/>
    

    Also, you probably don’t want to use an xsl:if here, because with your current template, it will not output text-body elements when the if condition is false. You should actually move the test to be part of the template match.

    <xsl:template match="main1/page/text-body[title/substring(.,1,1)='K']">
    

    Here’s the full XSLT

    <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/page/text-body[title/substring(.,1,1)='K']">
          <xsl:param name="count" select="title/substring(.,2,1)"/>
          <xsl:copy-of select="/root/main2/text-body[number($count)]"/>
       </xsl:template>
    </xsl:stylesheet>
    

    This should produce your expected output.

    If your are interested in a different approach, you could chose to define an xsl:key to look up the main2/text-body elements

    <xsl:key name="main2" 
         match="main2/text-body" 
         use="concat('K', count(preceding-sibling::text-body) + 1)" />
    

    That way, you can remove the need for substring by using the following template for matching the main1/page/text-body elements instead

     <xsl:template match="main1/page/text-body[key('main2', title)]">
        <xsl:copy-of select="key('main2', title)"/>
     </xsl:template>
    
    • 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> <text-body> <title>A</title>
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.