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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:07:12+00:00 2026-05-28T07:07:12+00:00

I have a problem, I can’t figure out the XSL code for transforming an

  • 0

I have a problem, I can’t figure out the XSL code for “transforming” an xml into another xml

This is the input xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<output>
  <cars>
    <car>
      <id>1</id>
      <brand>Audi</brand>
      <type>A3</type>
      <license>B-01-TST</license>
    </car>
    <car>
      <id>2</id>
      <brand>Volkwagen</brand>
      <type>Golf</type>
      <license>IF-02-TST</license>
    </car>
  </cars>
  <distances>
    <distance>
      <id_car>1</id_car>
      <date>20110901</date>
      <distance>111</distance>
    </distance>
    <distance>
      <id_car>1</id_car>
      <date>20110902</date>
      <distance>23</distance>
    </distance>
    <distance>
      <id_car>1</id_car>
      <date>20110903</date>
      <distance>0</distance>
    </distance>
    <distance>
      <id_car>2</id_car>
      <date>20110901</date>
      <distance>92</distance>
    </distance>
    <distance>
      <id_car>2</id_car>
      <date>20110902</date>
      <distance>87</distance>
    </distance>
    <distance>
      <id_car>2</id_car>
      <date>20110903</date>
      <distance>132</distance>
    </distance>
  </distances>
</output>

This is the output xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<cars>
  <car>
    <id>1</id> 
    <brand>Audi</brand>
    <type>A3</type>
    <license>B-01-TST</license>
    <distances>
      <distance day="20110901">111</distance>
      <distance day="20110902">23</distance>
      <distance day="20110903">0</distance>
    </distances>
  </car>
  <car>
    <id>2</id>
    <brand>Volkwagen</brand>
    <type>Golf</type>
    <license>IF-02-TST</license>
    <distances>
      <distance day="20110901">92</distance>
      <distance day="20110902">87</distance>
      <distance day="20110903">132</distance>
    </distances>
  </car>
</cars>

and the end for the element cars at the end I forgot.

Thank you.

  • 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-28T07:07:13+00:00Added an answer on May 28, 2026 at 7:07 am

    This is actually quite easy to do using XSLT; the hardest part is the use of keys. Here’s the code you need:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:key name="distances" match="distance" use="id_car" />
    
      <xsl:template match="output">
        <xsl:apply-templates select="cars" />
      </xsl:template>
    
      <xsl:template match="car">
        <xsl:copy>
          <xsl:apply-templates />
          <distances>
            <xsl:apply-templates select="key('distances', id)" />
          </distances>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="distance">
        <distance day="{date}">
          <xsl:value-of select="distance" />
        </distance>
      </xsl:template>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    The key essentially allows you to use the key function to get a list of all distance elements with an id_car of a given value.

    The first template handles the root and outputs only the cars element.

    The second template handles any car elements, outputting them exactly as they are, but adding in a distances element, and with the use of the key function, processes any distance elements with the correct ID.

    The last template is the ‘identity’ template that copies any elements that we haven’t already accounted for exactly as they are; this handles the brand, type, license elements, and so on.

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

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a weird problem that i can't figure out a solution for: I've
following up on this question , I now have another problem - how can
I have another problem I can't seem to solve..., or find on this site...
I have a problem - i can't compile SqlCipher. I'm using this http://groups.google.com/group/sqlcipher/browse_thread/thread/55c6296b56bf4533/c792bbec6df7d4f4?tvc=2#c792bbec6df7d4f4 instructions
hello today i have a problem how can this happen ? everything that is
i have this problem: as i can add list to one list? i have
I have one problem that I can't explain. Here is the code in main
i have this problem: user can add an HTML description and it will be
I have a problem and can't solve it alone. My teacher gives me one

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.