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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:20:56+00:00 2026-05-16T01:20:56+00:00

I need to merge certain xml nodes based on an attribute value, change that

  • 0

I need to merge certain xml nodes based on an attribute value, change that attribute value on the merged node and sum another attribute.

I am able to change the value of the attributes, but I couldn’t figure out how to sum(@count) and assign it to @count on the resulting xml

Source xml

<xml>
 <books category="X" count="2">
  <book name="bookx1"/>
  <book name="bookx2"/>
 </books>
 <books category="Y" count="3">
  <book name="booky1"/>
  <book name="booky2"/>
  <book name="booky3"/>
 </books>
 <books category="Z" count="2">
  <book name="bookz1"/>
  <book name="bookz2"/>
 </books></xml>

After xslt transform it needs to be like this

<xml>
 <books category="A" count="5">
  <book name="bookx1"/>
  <book name="bookx2"/>
  <book name="booky1"/>
  <book name="booky2"/>
  <book name="booky3"/>
 </books>
 <books category="Z" count="2">
  <book name="bookz1"/>
  <book name="bookz2"/>
 </books></xml>

This is my partial xslt

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml"/>
 <xsl:template match="*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
 </xsl:template>
 <xsl:template match="@*">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="@category">
  <xsl:attribute name="category">
   <xsl:choose>
    <xsl:when test=".='X'">
     <xsl:text>A</xsl:text>
    </xsl:when>
    <xsl:when test=".='Y'">
     <xsl:text>A</xsl:text>
    </xsl:when>
    <xsl:when test=".='Z'">
     <xsl:text>B</xsl:text>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="."/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:attribute>
 </xsl:template>

 <xsl:template match="books[@category='X']"/>
 <xsl:template match="books[@category='Y']"/></xsl:transform>
  • 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-16T01:20:57+00:00Added an answer on May 16, 2026 at 1:20 am

    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:key name="kBooksByCat" match="books"
        use="@category = 'Z'"/>
    
     <xsl:template match="node()|@*" name="identity">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <xml>
        <xsl:variable name="vNonZ"
          select="key('kBooksByCat', 'false')"/>
    
        <xsl:variable name="vCatZ"
          select="key('kBooksByCat', 'true')"/>
    
        <xsl:if test="$vNonZ">
         <books category="A" count="{sum($vNonZ/@count)}">
           <xsl:apply-templates select="$vNonZ/node()"/>
         </books>
        </xsl:if>
        <xsl:if test="$vCatZ">
         <books category="B" count="{sum($vCatZ/@count)}">
           <xsl:apply-templates select="$vCatZ/node()"/>
         </books>
        </xsl:if>
      </xml>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <xml>
     <books category="X" count="2">
      <book name="bookx1"/>
      <book name="bookx2"/>
     </books>
     <books category="Y" count="3">
      <book name="booky1"/>
      <book name="booky2"/>
      <book name="booky3"/>
     </books>
     <books category="Z" count="2">
      <book name="bookz1"/>
      <book name="bookz2"/>
     </books>
     </xml>
    

    produces the wanted, correct result:

    <xml>
       <books category="A" count="5">
          <book name="bookx1"/>
          <book name="bookx2"/>
          <book name="booky1"/>
          <book name="booky2"/>
          <book name="booky3"/>
       </books>
       <books category="B" count="2">
          <book name="bookz1"/>
          <book name="bookz2"/>
       </books>
    </xml>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I need to be able to merge two (very simple) JavaScript objects at runtime.
I need to merge a whole bunch of docx files programatically. Imagine they are
I have huge number of Word files I need to merge (join) into one
I need to do a simple mail merge in OpenOffice using C++, VBScript, VB.Net
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a way to allow sorting except for last item with in a list.
Need to locate the following pattern: The letter I followed by a space then
need ask you about some help. I have web app running in Net 2.0.

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.