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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:29:38+00:00 2026-06-04T20:29:38+00:00

I have the following XML input file: <mappings> <mapping> <key>6718</key> <value attribute=content_type>Info Page</value> </mapping>

  • 0

I have the following XML input file:

<mappings>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>35905</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>36941</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>24920</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>40244</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>36639</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>1861</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>2280</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>42062</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
</mappings>

I would like to produce the following XML:

<reductions>
    <reduction>
        <group>Info Page</group>
        <key>6718</key>
    </reduction>
    <reduction>
        <group>Press releases</group>
        <key>35905</key>
        <key>36941</key>
        <key>24920</key>
        <key>40244</key>
        <key>36639</key>
        <key>1861</key>
        <key>2280</key>
        <key>42062</key>
    </reduction>
</reductions>

So the keys from the input XML need to be grouped in the output XML based on the value of the value node from the input XML.
Also the duplicates need to be removed, the input XML contans the key with value 6718 twice, the output XML only once.

I’m using XSLT 2.0 with Saxon 9 HE.

Can someone please help me?

[EDIT]

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">

    <xsl:template match="/">
        <xsl:variable name="mappings">
            <mappings>
                <xsl:apply-templates select="resource/R"/>
            </mappings>
        </xsl:variable>
        <reductions>
            <xsl:apply-templates select="$mappings/*"/>
        </reductions>
    </xsl:template>

    <xsl:template match="R">
        <mapping>
            <key><xsl:value-of select="MT[@N='content_id']/@V"/></key>
            <value>
                <xsl:attribute name="attribute">content_type</xsl:attribute>
                <xsl:value-of select="MT[@N='content_type']/@V"/>
            </value>
        </mapping>
        <!--mapping>
            <key><xsl:value-of select="MT[@N='content_id']/@V"/></key>
            <value>
                <xsl:attribute name="attribute">modified_timestamp</xsl:attribute>
                <xsl:value-of select="format-dateTime(MT[@N='modified_timestamp']/@V, '[Y0001]-[M01]-[D01]')"/>
            </value>
        </mapping-->
    </xsl:template>

    <xsl:template match="mapping">
        <xsl:for-each-group select="." group-by="value">
            <reduction>
                <group><xsl:value-of select="current-grouping-key()"/></group>
                <xsl:for-each-group select="current-group()/key" group-by=".">
                    <xsl:copy-of select="."/>
                </xsl:for-each-group>
            </reduction>
        </xsl:for-each-group>
    </xsl:template>

</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-06-04T20:29:41+00:00Added an answer on June 4, 2026 at 8:29 pm

    Here is a stylesheet using for-each-group twice:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="mappings">
      <reductions>
        <xsl:for-each-group select="mapping" group-by="value">
          <reduction>
            <group><xsl:value-of select="current-grouping-key()"/></group>
            <xsl:for-each-group select="current-group()/key" group-by=".">
              <xsl:copy-of select="."/>
            </xsl:for-each-group>
          </reduction>
        </xsl:for-each-group>
      </reductions>
    </xsl:template>
    
    </xsl:stylesheet>
    

    [edit]
    When I run the posted stylesheet with Saxon 9.4 HE against the input

    <mappings>
        <mapping>
            <key>6718</key>
            <value attribute="content_type">Info Page</value>
        </mapping>
        <mapping>
            <key>35905</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>6718</key>
            <value attribute="content_type">Info Page</value>
        </mapping>
        <mapping>
            <key>36941</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>24920</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>40244</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>36639</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>1861</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>2280</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
        <mapping>
            <key>42062</key>
            <value attribute="content_type">Press releases</value>
        </mapping>
    </mappings>
    

    I get the result

    <reductions>
       <reduction>
          <group>Info Page</group>
          <key>6718</key>
       </reduction>
       <reduction>
          <group>Press releases</group>
          <key>35905</key>
          <key>36941</key>
          <key>24920</key>
          <key>40244</key>
          <key>36639</key>
          <key>1861</key>
          <key>2280</key>
          <key>42062</key>
       </reduction>
    </reductions>
    

    which is correct as far as I understand your requirements.

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

Sidebar

Related Questions

In my XSLT file, I have the following: <input type=button value= <xsl:value-of select=name>> It's
i have following xml file as input .... <?xml version=1.0 encoding=ISO-8859-1?> <T0020 xsi:schemaLocation=http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd
I have the following input XML: <root age=1> <description>some text</description> <section> <item name=a> <uuid>1</uuid>
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have the following very simple XML file and I want to quickly parse
I have a sample htm file with following structure and it POSTs the xml
I have an interesting problem with XSL. We have an XML Input file that
Given an input xml file with following structure: <root> <record row=1 col=1 val=1 />
I Have following in my struts.xml file <action name=ProductVerification class=com.frontend.ProductVerification> <result name=success>/jsp/product_verification.jsp</result> <result name=input>/jsp/product_verification.jsp</result>
I have the following XML file: <?xml version='1.0' encoding='utf-8'?> <Answers> <Question1 q=What is your

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.