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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:09:20+00:00 2026-05-27T17:09:20+00:00

I have some source XML containing different elements that get transformed by a stylesheet

  • 0

I have some source XML containing different elements that get transformed by a stylesheet into output elements with the same name. The output elements then require an ‘index’ element on each one.
My first choice was using position(), but of course that only works for the context of the xsl:for-each in which they’re positioned.

Here is a sample XML:

<Root>
    <TypeA Val="First Value"/>
    <TypeA Val="Second Value"/>
    <NotToBeTransformed Val="ignoreme"/>
    <TypeB Val="Third Value"/>
    <TypeB Val="Fourth Value"/>
</Root>

And here is how I would like it to be rendered as output:

<Output>
    <Destination>
        <Index>1</Index>
        <Content>First Value</Content>
    </Destination>
    <Destination>
        <Index>2</Index>
        <Content>Second Value</Content>
    </Destination>
    <Destination>
        <Index>3</Index>
        <Content>Third Value</Content>
    </Destination>
    <Destination>
        <Index>4</Index>
        <Content>Fourth Value</Content>
    </Destination>
</Output>

My simple XSLT is as follows:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
    <Output>
        <xsl:for-each select="//TypeA">
            <Destination>
                <Index><xsl:value-of select="position()"/></Index>
                <Content><xsl:value-of select="@Val"/></Content>
            </Destination>
        </xsl:for-each>
        <xsl:for-each select="//TypeB">
            <Destination>
                <Index><xsl:value-of select="position()"/></Index>
                <Content><xsl:value-of select="@Val"/></Content>
            </Destination>
        </xsl:for-each>
    </Output>
</xsl:template>

But that outputs the Index with values of 1 and 2 for each of the collections of source elements.
Can this be done in XSLT or would this need to be processed again? I’m doing the transform with C# code, so I could run an initial transform and then set the Index values in code afterwards.

Any help would be most gratefully accepted!

  • 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-27T17:09:21+00:00Added an answer on May 27, 2026 at 5:09 pm

    You don’t need to use xsl:for-each at all (try always to avoid it), neither it is necessary to process TypeA and TypeB elements separately.

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <Output>
       <xsl:apply-templates select="TypeA|TypeB"/>
      </Output>
     </xsl:template>
     <xsl:template match="*/*">
        <Destination>
            <Index><xsl:value-of select="position()"/></Index>
            <Content><xsl:value-of select="@Val"/></Content>
        </Destination>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Root>
        <TypeA Val="First Value"/>
        <TypeA Val="Second Value"/>
        <NotToBeTransformed Val="ignoreme"/>
        <TypeB Val="Third Value"/>
        <TypeB Val="Fourth Value"/>
    </Root>
    

    produces the wanted, correct result:

    <Output>
       <Destination>
          <Index>1</Index>
          <Content>First Value</Content>
       </Destination>
       <Destination>
          <Index>2</Index>
          <Content>Second Value</Content>
       </Destination>
       <Destination>
          <Index>3</Index>
          <Content>Third Value</Content>
       </Destination>
       <Destination>
          <Index>4</Index>
          <Content>Fourth Value</Content>
       </Destination>
    </Output>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some source code to get the file name of an url for
I'm not even sure if it's possible but say I have some XML: <source>
I have some source files that have comments written in Japanese. When I open
I have some HTML errors in my output source code, coming from one of
I have some XML text that I wish to render in an HTML page.
I have some Java code that validates XML against an XSD. I am using
I need some help about xsl. I have a date attribute in source xml
I have some XML that has an extra element and I want it gone.
I have some XML to work with, something like this: <admin_list> <admin> <name>user1</name> <authentication_source>Local</authentication_source>
I have some xml files that will be read-only that I need to access

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.