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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:00:35+00:00 2026-05-27T20:00:35+00:00

How can I modularize a repeated set of outputs in an xslt transform? For

  • 0

How can I modularize a repeated set of outputs in an xslt transform? For example I have something like the following (pseudo code).

<foreach "path">
  <if "count(/subPath) = 0">
    <a><value-of "x"/></a>
    <b><value-of "y"/></b>
    <c></c>
  </fi>
  <else>
    <foreach "subPath">
      <a><value-of "../x"/></a>
      <b><value-of "../y"/></b>
      <c><value-of "z"/></c>
    </foreach>
  </else>
</foreach>

and would like something like the following

<foreach "path">
  <if "count(/subPath) = 0">
     <?/>
  </fi>
  <else>
    <foreach "subPath">
       <?/>
    </foreach>
  </else>
</foreach> 
<?>
   <a><value-of "../x"/></a>
   <b><value-of "../y"/></b>
   <c><value-of "z"/></c>
</?>

What construct am I looking for?

  • 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-27T20:00:35+00:00Added an answer on May 27, 2026 at 8:00 pm

    I. Pseudo-code translation

    Your pseudocode translates 1:1 to this XSLT transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
         <xsl:for-each select="/t/m/n/p">
           <xsl:choose>
            <xsl:when test="not(subPath)">
            <a><xsl:value-of select="x"/></a>
            <b><xsl:value-of select="y"/></b>   
          </xsl:when>
          <xsl:otherwise>
           <xsl:for-each select="subPath">
            <a><xsl:value-of select="../x"/></a>
            <b><xsl:value-of select="../y"/></b>
            <c><xsl:value-of select="z"/></c>   
           </xsl:for-each>
          </xsl:otherwise>
           </xsl:choose>
         </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document (no such document is provided in the question!!!):

    <t>
     <m>
      <n>
       <p>
         <x>1</x>
         <y>2</y>
         <subPath>
           <z>3</z>
         </subPath>
         <subPath>
           <z>4</z>
         </subPath>
       </p>
      </n>
     </m>
    </t>
    

    the wanted, correct result is produced:

    <a>1</a>
    <b>2</b>
    <c>3</c>
    <a>1</a>
    <b>2</b>
    <c>4</c>
    

    II. Refactoring:

    This is a refactored, equivalent transformation that doesn’t use any xsl:for-each or any explicit conditional instructions:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="p[not(subPath)]" name="processP">
       <xsl:param name="pNode" select="."/>
       <a><xsl:value-of select="$pNode/x"/></a>
       <b><xsl:value-of select="$pNode/y"/></b>
     </xsl:template>
    
     <xsl:template match="p/subPath">
       <xsl:call-template name="processP">
        <xsl:with-param name="pNode" select=".."/>
       </xsl:call-template>
       <c><xsl:value-of select="z"/></c>    
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the same XML document (above), again the same correct result is produced:

    <a>1</a>
    <b>2</b>
    <c>3</c>
    <a>1</a>
    <b>2</b>
    <c>4</c>
    

    Do note:

    1. Templates/pattern matching to avoid using explicit conditional instructions. This is the most powerful, XSLT-specific design pattern.

    2. There is no duplicate code.

    3. Each templates matches exactly the node it has to process, under the exact required conditions.

    4. The code of the first template is re-used by also giving it a name, so that it can be explicitly called. In this way code duplication is avoided.

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

Sidebar

Related Questions

Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Within my C# code behind page, I have the following: MembershipUser currentUser = Membership.GetUser(false);
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
I have a classic ASP application that I've been constantly trying to modularize. Currently,
I have 3 TFS Builds (2 Dev and 1 Cert). I would like to
I can't get facelets file to load into a template. I have a content
Let me explain: I know that, in Java , you can do stuff like
When creating a StructureMap container, I typically do something like this: var container =
Can anyone let me know how can we change the value of kendo combobox

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.