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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:33:14+00:00 2026-06-18T20:33:14+00:00

I want to do a transformation from XML to text combining some elements, but

  • 0

I want to do a transformation from XML to text combining some elements, but avoiding duplicates in the output.
The XML would be something like that:

<A>
  <B>
    <param1>value0</param1>
    <param2>value1</param2>
  </B>
  <B>
    <param1>value2</param1>
    <param2>value3</param2>
  </B>
  <C>
    <param3>valueC1</param3>
    <D>
      <param4>value0</param4>
      <param5>value4</param5>
    </D>
    <D>
      <param4>value0</param4>
      <param5>value5</param5>
    </D>
    <D>
      <param4>value2</param4>
      <param5>value6</param5>
    </D>
  </C>
  <C>
    <param3>valueC2</param3>
    <D>
      <param4>value0</param4>
      <param5>value5</param5>
    </D>
  </C>
</A>

And the output:

OBJECT: param1=value0, param2=value1, param3=valueC1, param4=value0;
OBJECT: param1=value2, param2=value3, param3=valueC1, param4=value2;
OBJECT: param1=value0, param2=value1, param3=valueC2, param4=value0;

Notes:

  • For every D object, look for a match with B objects using D.param4 = B.param1
  • If there are two or more D objects into the same C and matching with the same B, print only one of them (in the example, nothing is done with the second D object because it would produce the same line that the first one)
  • If there are two D objects matching with the same B, but into different C’s, print both (third row in the output example)

I looked for some similar question, but I couldn’t find any in the same case.

I guess that it could be done by using keys, but it’s too complex.

Thanks!

Regards,
Ale.

PS: Sorry for my English.

  • 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-18T20:33:15+00:00Added an answer on June 18, 2026 at 8:33 pm

    Given that you’re not making any use of param5 in your output it would appear to be possible to simplify the problem to

    • for each C
      • find all distinct B elements whose param1 matches the param4 of any of the contained Ds
      • for each of those
        • extract B/param1, B/param2, currentC/param3, B/param1 again (but labelled param4)

    This is one way to achieve that using templates.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="text" />
      <xsl:key name="BbyParam1" match="B" use="param1" />
    
      <xsl:template match="/">
        <xsl:apply-templates select="A/C" />
      </xsl:template>
    
      <xsl:template match="C">
        <xsl:apply-templates select="key('BbyParam1', D/param4)">
          <xsl:with-param name="currentC" select="." />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="B">
        <xsl:param name="currentC" />
        <xsl:text>OBJECT: param1=</xsl:text>
        <xsl:value-of select="param1" />
        <xsl:text>, param2=</xsl:text>
        <xsl:value-of select="param2" />
        <xsl:text>, param3=</xsl:text>
        <xsl:value-of select="$currentC/param3" />
        <xsl:text>, param4=</xsl:text>
        <xsl:value-of select="param1" />
        <xsl:text>&#10;</xsl:text>
      </xsl:template>
    </xsl:stylesheet>
    

    “find all distinct B elements whose param1 matches the param4 of any of the contained Ds” is actually very straightforward due to the fact that when you pass a node set as the second argument to the key function it does precisely this – it returns the set of all nodes whose key value is the string value of any of the nodes in the argument node set, and the returned node set (being a set) is guaranteed to contain no duplicates.

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

Sidebar

Related Questions

My output from this transformation only includes the text, but I want the HTML
I have some problems excluding unwanted output from my xlst transformation. I already know
I am getting an XML document output from Cognos which I want to use
I'd like to take data from some XML files and transform them into a
I want to make a fourier-transformation of an image. But how can I change
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I have a class that consume an XML file and produce text output based
I have been doing some data transformation/processing in PL/SQL and I want to eliminate
I am using Transformer to perform XSL transformation from XML to XHTML: import javax.xml.transform.Transformer;
I'm doing a xstl transformation with saxon from an XML document. The doc is

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.