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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:31:26+00:00 2026-05-30T20:31:26+00:00

I have a source document with XML structure similar to this: <FOO> <BAR>x</BAR> <BAR>y</BAR>

  • 0

I have a source document with XML structure similar to this:

<FOO>
     <BAR>x</BAR>
     <BAR>y</BAR>
     <BAR>z</BAR>
</FOO>

My target XML must have an attribute with a number that represents the numerical sum of x, y and z. Where x = 1, y = 2 and z = 3

NOTE: The x, y and z are not actually numbers in the source document. They are letters and need to be mapped to the numbers that they represent first.

In this case, the target should look something like:

<Target Sum=6>
</Target>

Anyone have an XSLT example that would do what I need?

Thanks in advance

  • 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-30T20:31:27+00:00Added an answer on May 30, 2026 at 8:31 pm
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
      <Target Sum="{sum(FOO/BAR)}" />
    </xsl:template>
    

    EDIT:

    This is a bit verbose, and there is probably a more elegant way to do it, but essentially I have a named template here that recursively calls itself to calculate the sum after the hard-coded mapping occurs:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/">
        <Target>
          <xsl:attribute name="Sum">
            <xsl:call-template name="GetSum">
              <xsl:with-param name="CurrentNode" select="FOO/BAR[position()=1]"/>
            </xsl:call-template>
         </xsl:attribute>
        </Target>
      </xsl:template>
    
      <xsl:template name="GetSum">
        <xsl:param name="CurrentNode"/>
        <xsl:param name="Number" select="0"/>
    
        <xsl:variable name="Recursive_Result">
          <xsl:variable name="MappedNumber">
            <xsl:choose>
              <xsl:when test="$CurrentNode/. = 'x'">1</xsl:when>
              <xsl:when test="$CurrentNode/. = 'y'">2</xsl:when>
              <xsl:when test="$CurrentNode/. = 'z'">3</xsl:when>
              <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
    
          <!-- if there is a next sibling, recursively call GetSum  -->
          <xsl:choose>
            <xsl:when test="$CurrentNode/following-sibling::BAR[1]">
              <xsl:call-template name="GetSum">
                <xsl:with-param name="CurrentNode" select="$CurrentNode/following-sibling::BAR[1]"/>
                <xsl:with-param name="Number">
                  <xsl:value-of select="$MappedNumber"/>
                </xsl:with-param>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$MappedNumber"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
    
        <!-- add the recursive_result to the number passed into the template. this will eventually build a sum -->
        <xsl:value-of select="$Recursive_Result + $Number"/>
      </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 an XML document (an InfoPath form) that looks similar to this: <my:ClientMaintenance
Have a source xml document that uses namespace containing prefixes and a default namespace.
I have an Xml document that looks similar too <Reports xmlns=> <Report> <ReportID>1</ReportID> <ParameterTemplate
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
I have a source XML file that is used to create C# files that
In this case I have XML data source and external images files whole together
Basically I have a script that updates an xml document in various places... However,
Using this source document: <?xml version=1.0 encoding=UTF-8?> <Root> <Element1 id=UniqueId1> <SubElement1/> <SubElement2> <LeafElement1/> <LeafElement1/>
I have a WIX XML document that contains 2,000+ file tags. I am trying
This is a fairly simple case. I have an xml document, linked to an

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.