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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:47:06+00:00 2026-05-15T07:47:06+00:00

Here is my contrived example that illustrates what I am attempting to accomplish. I

  • 0

Here is my contrived example that illustrates what I am attempting to accomplish. I have an input XML file that I wish to flatten for further processing.

Input file:

<BICYCLES>
    <BICYCLE>
        <COLOR>BLUE</COLOR>
        <WHEELS>
            <WHEEL>
                <WHEEL_TYPE>FRONT</WHEEL_TYPE>
                <FLAT>NO</FLAT>
                <REFLECTORS>
                    <REFLECTOR>
                        <REFLECTOR_NUM>1</REFLECTOR_NUM>
                        <SHAPE>SQUARE</SHAPE>
                    </REFLECTOR>
                    <REFLECTOR>
                        <REFLECTOR_NUM>2</REFLECTOR_NUM>
                        <SHAPE>ROUND</SHAPE>
                    </REFLECTOR>
                </REFLECTORS>
            </WHEEL>
            <WHEEL>
                <WHEEL_TYPE>REAR</WHEEL_TYPE>
                <FLAT>NO</FLAT>
            </WHEEL>
        </WHEELS>
    </BICYCLE>
</BICYCLES>

The input is a list of <BICYCLE> nodes. Each <BICYCLE> has a <COLOR> and optionally has <WHEELS>.

<WHEELS> is a list of <WHEEL> nodes, each of which has a few attributes, and optionally has <REFLECTORS>.

<REFLECTORS> is a list of <REFLECTOR> nodes, each of which has a few attributes.

The goal is to flatten this XML. This is the XSL I’m using:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" xml:space="preserve"/> 

<xsl:template match="/">
<BICYCLES>
<xsl:apply-templates/>
</BICYCLES>
</xsl:template>

<xsl:template match="BICYCLE">
<xsl:choose>
    <xsl:when test="WHEELS">
        <xsl:apply-templates select="WHEELS"/>
    </xsl:when>
    <xsl:otherwise>
        <BICYCLE>
            <COLOR><xsl:value-of select="COLOR"/></COLOR>
            <WHEEL_TYPE/>
            <FLAT/>
            <REFLECTOR_NUM/>
            <SHAPE/>
        </BICYCLE>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="WHEELS">
<xsl:apply-templates select="WHEEL"/>
</xsl:template>

<xsl:template match="WHEEL">
    <xsl:choose>
        <xsl:when test="REFLECTORS">
            <xsl:apply-templates select="REFLECTORS"/>
        </xsl:when>
        <xsl:otherwise>
            <BICYCLE>
                <COLOR><xsl:value-of select="../../COLOR"/></COLOR>
                <WHEEL_TYPE><xsl:value-of select="WHEEL_TYPE"/></WHEEL_TYPE>
                <FLAT><xsl:value-of select="FLAT"/></FLAT>
                <REFLECTOR_NUM/>
                <SHAPE/>
            </BICYCLE>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="REFLECTORS">
    <xsl:apply-templates select="REFLECTOR"/>
</xsl:template>

<xsl:template match="REFLECTOR"> 
    <BICYCLE>
        <COLOR><xsl:value-of select="../../../../COLOR"/></COLOR>
        <WHEEL_TYPE><xsl:value-of select="../../WHEEL_TYPE"/></WHEEL_TYPE>
        <FLAT><xsl:value-of select="../../FLAT"/></FLAT>
        <REFLECTOR_NUM><xsl:value-of select="REFLECTOR_NUM"/></REFLECTOR_NUM>
        <SHAPE><xsl:value-of select="SHAPE"/></SHAPE>
    </BICYCLE>
</xsl:template>

</xsl:stylesheet>

The output is:

<BICYCLES xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <BICYCLE>
        <COLOR>BLUE</COLOR>
        <WHEEL_TYPE>FRONT</WHEEL_TYPE>
        <FLAT>NO</FLAT>
        <REFLECTOR_NUM>1</REFLECTOR_NUM>
        <SHAPE>SQUARE</SHAPE>
    </BICYCLE>
    <BICYCLE>
        <COLOR>BLUE</COLOR>
        <WHEEL_TYPE>FRONT</WHEEL_TYPE>
        <FLAT>NO</FLAT>
        <REFLECTOR_NUM>2</REFLECTOR_NUM>
        <SHAPE>ROUND</SHAPE>
    </BICYCLE>
    <BICYCLE>
        <COLOR>BLUE</COLOR>
        <WHEEL_TYPE>REAR</WHEEL_TYPE>
        <FLAT>NO</FLAT>
        <REFLECTOR_NUM/>
        <SHAPE/>
    </BICYCLE>
</BICYCLES>

What I don’t like about this is that I’m outputting the color attribute in several forms:

<COLOR><xsl:value-of select="../../../../COLOR"/></COLOR>
<COLOR><xsl:value-of select="../../COLOR"/></COLOR>
<COLOR><xsl:value-of select="COLOR"/></COLOR>
<COLOR/>

It seems like there ought to be a way to make a named template and invoke it from the various places where it is needed and pass some parameter that represents the path back to the <BICYCLE> node to which it refers.

Is there a way to clean this up, say with a named template for bicycle fields, for wheel fields and for reflector fields?

In the real world example this is based on, there are many more attributes to a “bicycle” than just color, and I want to make this XSL easy to change to include or exclude fields without having to change the XSL in multiple places.

  • 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-15T07:47:06+00:00Added an answer on May 15, 2026 at 7:47 am

    You can name templates by using the name attribute. You invoke a template by name using <xsl:call-template>, and it’s valid (IIRC) anywhere <xsl:apply-templates> is valid.

    UPDATE (from the comments): Sounds like you want a different axis, probably ancestor. Something like ancestor::bicycle/color?

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

Sidebar

Related Questions

To use a contrived example in Java, here's the code: enum Commands{ Save(S); File(F);
Here is a contrived example of an xml document. In my real world case,
So here's a contrived example of what I want to have improved or confirmed.
In python, you can have a function return multiple values. Here's a contrived example:
I have two models that are related to each other. For a contrived example,
Here's a contrived example: I want to divide two numbers and I need to
Here's a contrived example of my code: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
I have the following contrived example in Rails. I want to make sure the
I have the following contrived example (coming from real code): template <class T> class
This is a somewhat contrived example meant to show a point. Here are two

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.