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

Ask A Question

Stats

  • Questions 469k
  • Answers 469k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming the list is named contacts, here is a linq… May 16, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer Part of your problem is that you have compounded so… May 16, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer Use String.replace() as needed: "[^\\\"]*\\".replace("\"", "&quot;"); May 16, 2026 at 2:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.