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

  • Home
  • SEARCH
  • 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 8049257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:26+00:00 2026-06-05T06:44:26+00:00

I need to transform an XML file with XSLT, and this task is kinda

  • 0

I need to transform an XML file with XSLT, and this task is kinda tricky.

I have attributes with the name attr_1000_a whereas the number and the suffix are dynamic, so that attr_2000_b is valid too.

Further, there are <row> elements which combine related data. I need to transform them so that equally numbered attributes (i.e. attr_1000_a and attr_1000_b) are put into the same element.

Let me give you an example. Following input XML:

<root>
  <row id="1">
    <foo attr_1000_a="true">1</foo>
    <foo attr_1000_b="true">2</foo>
    <foo attr_1000_c="true">3</foo>
  </row>
  <row id="2">
    <foo attr_1000_a="true" attr_1000_b="true" attr_1000_c="true">10</foo>
    <foo attr_2000_a="true" attr_2000_b="true" attr_2000_c="true">20</foo>
  </row>
  <row id="3">
    <foo attr_1000_a="true" attr_2000_a="true" attr_3000_a="true">100</foo>
    <foo attr_1000_b="true" attr_2000_b="true" attr_3000_b="true">200</foo>
    <foo attr_1000_c="true" attr_2000_c="true" attr_3000_c="true">300</foo>
  </row>
</root>

You can see that the attributes can be combined in several ways, which makes the transformation difficult. Each attribute is unique in each <row> but can be located in any <foo> element. Also, each <foo> can have an arbitrary number of attributes.

The desired result:

<result>
  <row id="1">
    <field attr="1000">
      <a>1</a>
      <b>2</b>
      <c>3</c>
    </field>
  </row>
  <row id="2">
    <field attr="1000">
      <a>10</a>
      <b>10</b>
      <c>10</c>
    </field>
    <field attr="2000">
      <a>20</a>
      <b>20</b>
      <c>20</c>
    </field>
  </row>
  <row id="3">
    <field attr="1000">
      <a>100</a>
      <b>200</b>
      <c>300</c>
    </field>
    <field attr="2000">
      <a>100</a>
      <b>200</b>
      <c>300</c>
    </field>
    <field attr="3000">
      <a>100</a>
      <b>200</b>
      <c>300</c>
    </field>
  </row>
</result>

I think i have to somehow get a list of all numbers in a row (for example 1000, 2000 and 3000) and then iterate through all elements that have such an attriute.

How can i do this using XSLT? Is this even possible?

  • 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-05T06:44:27+00:00Added an answer on June 5, 2026 at 6:44 am

    Quick and Dirty, this xslt

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:template match="root">
            <result>
             <xsl:apply-templates select="@*|node()"/>
            </result>
        </xsl:template>
    
        <xsl:template match="foo/@*">
            <xsl:element name="{substring-after(local-name(),'000_')}">
                <xsl:value-of select=".."/>
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="row">
            <row id="{@id}">
                <xsl:for-each-group select="foo/@*" group-by="substring(local-name(),1,9)">
                    <field attr="{substring-after(current-grouping-key(),'attr_')}">
                       <xsl:apply-templates select="current-group()"/>
                    </field>
                </xsl:for-each-group>
            </row>
        </xsl:template>
    
        <xsl:template match="foo">
            <xsl:apply-templates  select="@*"/>
        </xsl:template>
    
         <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>   
    </xsl:stylesheet>
    

    applied to this input

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <row id="1">
            <foo attr_1000_a="true">1</foo>
            <foo attr_1000_b="true">2</foo>
            <foo attr_1000_c="true">3</foo>
        </row>
        <row id="2">
            <foo attr_1000_a="true" attr_1000_b="true" attr_1000_c="true">10</foo>
            <foo attr_2000_a="true" attr_2000_b="true" attr_2000_c="true">20</foo>
        </row>
        <row id="3">
            <foo attr_1000_a="true" attr_2000_a="true" attr_3000_a="true">100</foo>
            <foo attr_1000_b="true" attr_2000_b="true" attr_3000_b="true">200</foo>
            <foo attr_1000_c="true" attr_2000_c="true" attr_3000_c="true">300</foo>
        </row>
    </root>
    

    yields this result

    <?xml version="1.0" encoding="UTF-8"?>
    <result>
        <row id="1">
          <field attr="1000">
             <a>1</a>
             <b>2</b>
             <c>3</c>
          </field>
       </row>
        <row id="2">
          <field attr="1000">
             <a>10</a>
             <b>10</b>
             <c>10</c>
          </field>
          <field attr="2000">
             <a>20</a>
             <b>20</b>
             <c>20</c>
          </field>
       </row>
        <row id="3">
          <field attr="1000">
             <a>100</a>
             <b>200</b>
             <c>300</c>
          </field>
          <field attr="2000">
             <a>100</a>
             <b>200</b>
             <c>300</c>
          </field>
          <field attr="3000">
             <a>100</a>
             <b>200</b>
             <c>300</c>
          </field>
       </row>
    </result>
    

    the magic is in

        <xsl:element name="{substring-after(local-name(),'000_')}">
            <xsl:value-of select=".."/>
        </xsl:element>
    

    this creates the a/b/c elements with a dynamic name and travels up one node to get the value from the parent node (we’re currently in the attribute).

    and in

            <xsl:for-each-group select="foo/@*" group-by="substring(local-name(),1,9)">
                <field attr="{substring-after(current-grouping-key(),'attr_')}">
                   <xsl:apply-templates select="current-group()"/>
                </field>
            </xsl:for-each-group>
    

    which regroups all attributes (foo/@*) using part of their name (substring(local-name(),1,9)). The first are subsequently available as current-group(), the latter as current-grouping-key(), as you can see.

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

Sidebar

Related Questions

I need to have my XSLT stylesheet sort my XML file's child nodes, but
For XSLT transformation I need a javax.xml.transform.stream.StreamSource object representing xml file being transformed. I
I have a list (list1) with the file names: C:\\Work\\Server1\\CSRegWeb\\Transform\\Faq.xslt C:\\Work\\Server1\\CSRegWeb\\Content\\Axxess.xml C:\\Work\\Server1\\CSRegWeb\\kleenex.aspx C:\\Work\\Server1\\CSRegWeb\\Content\\dell.xml I
I have an XSLT transform I am using to process an XML file, inserting
I need xslt to transform, My Xml is as below <OrderReferences> <OrderRef> <OrderRef>OrderRef1</OrderRef> <Type>ERP</Type>
I need to transform an XML file to another XML file, where the source
I have a XML file that I need to populate multiple SQL tables, and
I have a requirement to run a transform on a xml file. it is
I need to transform an SVG file to HTML5 canvas, through XSLT. The problem
I have a XML which works with an XSLT file that transforms the XML

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.