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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:04:14+00:00 2026-06-07T13:04:14+00:00

If I have this input: <root> <library id=L1> <shelf1 id=1> <book id=1 category=science> <attributes>

  • 0

If I have this input:

<root> 
    <library id="L1">
        <shelf1 id="1">
            <book id="1" category="science">
                <attributes>                    
                    <year>2000</year>
                </attributes>
                <attributes>
                    <author>xx</author>
                    <year>2010</year>
                    <isbn>001</isbn>
                </attributes>
                <attributes>
                    <author>yy</author>
                    <publisher>zz</publisher>
                    <isbn>002</isbn>
                </attributes>
                <other>y</other>
            </book>  
            <book id="2" category="science">
                ...
            </book>
        </shelf1>

        <shelf2>...</shelf2>
    </library>
</root>

expetced output:

<root> 
    <library id="L1">
        <shelf1 id="1">
            <book id="1" category="science">
                <attributes>
                    <author>yy</author>                    
                    <publisher>zz</publisher>
                    <isbn>002</isbn>
                    <year>2010</year>
                </attributes>
                <other>y</other>
            </book>  
            <book id="2" category="science">
                ...
            </book>
        </shelf1>

        <shelf2>...</shelf2>
    </library>
</root>

I need to combine the element ‘attributes’ together. If two or more attributes exist,
when the child of the attributes never exist before, we keep the child as new information, but if it exists previously we simply use the latest value.

How to do such transformation in XSLT 1.0 or 2.0?Thanks very much for the help.

John

  • 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-07T13:04:16+00:00Added an answer on June 7, 2026 at 1:04 pm

    This transformation (XSLT 2.0):

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="book[attributes]">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <attributes>
          <xsl:for-each-group select="attributes/*" group-by="name()">
            <xsl:sort select="current-grouping-key()"/>
            <xsl:apply-templates select="."/>
          </xsl:for-each-group>
        </attributes>
        <xsl:apply-templates select="*[not(self::attributes)]"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <root>
        <library id="L1">
            <shelf1 id="1">
                <book id="1" category="science">
                    <attributes>
                        <year>2000</year>
                    </attributes>
                    <attributes>
                        <author>xx</author>
                        <year>2010</year>
                        <isbn>001</isbn>
                    </attributes>
                    <attributes>
                        <author>yy</author>
                        <publisher>zz</publisher>
                        <isbn>002</isbn>
                    </attributes>
                    <other>y</other>
                </book>
                <book id="2" category="science">
                ...
                </book>
            </shelf1>
            <shelf2>...</shelf2>
        </library>
    </root>
    

    produces the wanted, correct result:

    <root>
          <library id="L1">
                <shelf1 id="1">
                      <book id="1" category="science">
                <attributes>
                   <author>xx</author>
                   <isbn>001</isbn>
                   <publisher>zz</publisher>
                   <year>2000</year>
                </attributes>
                <other>y</other>
             </book>
                      <book id="2" category="science">
                ...
                </book>
                </shelf1>
                <shelf2>...</shelf2>
          </library>
    </root>
    

    Explanation:

    1. Proper use and overriding of the identity rule.

    2. Proper use of the XSLT 2.0 instruction xsl:for-each-group with attribute group-by.

    3. Proper use of the XSLT 2.0 function current-grouping-key() and the XPath function name().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem for xslt: This is the input file: <root> <header/> <item/>
I have this input XML which needs to be transformed with an xslt <root>
If I have this input file in xml: <root> <node id=N1> <fruit id=1> <orange
I have the following XML <ns0:Root xmlns:ns0=http://Map_Sample.Input_Schema> <Number1> </Number1> <Number2>11</Number2> <Number3>12</Number3> </ns0:Root> In this
I have this input text and an input button...I am looking to have them
I have this input text and input button, I am trying to get them
I have this input form which goes like this: <input type=checkbox name=vehicle value=Bike />
I have this select input with simple_form: <%= f.input :board, :collection => Board.where(:user_id =>
I have this select input in my form that I want to access via
I have this: $(document).ready(function() { $(input[type=button]).click(function () { $(#test).html(W3Schools); alert(Would submit: + $(this).siblings(input[type=text]).val()); $.ajax({

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.