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

  • Home
  • SEARCH
  • 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 9233647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:36:16+00:00 2026-06-18T06:36:16+00:00

I need to format XML input using XSL to obtain more convenient structure. As

  • 0

I need to format XML input using XSL to obtain more convenient structure. As a next step of processing i want to transform it to HTML.
Suppose i have the following input: (0)

<list>
<item item-id="1" second-item-id="1" third-item-id="1"/>
<item item-id="1" second-item-id="1" third-item-id="2"/>
<item item-id="1" second-item-id="2" third-item-id="1"/>
<item item-id="1" second-item-id="3" third-item-id="1"/>

<item item-id="2" second-item-id="1" third-item-id="1"/>
<item item-id="2" second-item-id="1" third-item-id="2"/>
<item item-id="2" second-item-id="1" third-item-id="3"/>
<item item-id="2" second-item-id="2" third-item-id="1"/>

<item item-id="3" second-item-id="1" third-item-id="1"/>
<item item-id="3" second-item-id="1" third-item-id="2"/>
<item item-id="3" second-item-id="1" third-item-id="3"/>
<item item-id="3" second-item-id="1" third-item-id="4"/>
</list>

and the following XSL template: (1)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="itemKey" match="item" use="@item-id"/>
  <xsl:key name="secondItemKey" match="item" use="concat(@item-id, '|', @second-item-id)"/>

  <xsl:template match="list">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="item[generate-id() = generate-id(key('itemKey', @item-id)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <item item-id="{@item-id}">
      <xsl:apply-templates select="key('itemKey', @item-id)[generate-id() = generate-id(key('secondItemKey', concat(@item-id, '|', @second-item-id))[1])]" mode="evt"/>
    </item>
  </xsl:template>

  <xsl:template match="item" mode="evt">
    <second-item second-item-id="{@second-item-id}">
      <xsl:apply-templates select="key('secondItemKey', concat(@item-id, '|', @second-item-id))" mode="bus"/>
    </second-item>
  </xsl:template>

  <xsl:template match="item" mode="bus">
    <third-item third-item-id="{@third-item-id}"/>
  </xsl:template>    

</xsl:stylesheet>

it gives me pretty fine XML: (2)

<?xml version="1.0"?>
<list>
    <item item-id="1">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
        </second-item>
        <second-item second-item-id="2">
            <third-item third-item-id="1"/>
        </second-item>
        <second-item second-item-id="3">
            <third-item third-item-id="1"/>
        </second-item>
    </item>
    <item item-id="2">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
            <third-item third-item-id="3"/>
        </second-item>
        <second-item second-item-id="2">
            <third-item third-item-id="1"/>
        </second-item>
    </item>
    <item item-id="3">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
            <third-item third-item-id="3"/>
            <third-item third-item-id="4"/>
        </second-item>
    </item>
</list>

i have another XSL which transforms XML #2 to html:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="html"/>
    <xsl:template match="list">
        <xsl:for-each select="item">
            <h2><xsl:value-of select="concat(local-name(),' ',@item-id)"/></h2>
            <ul>
                <xsl:for-each select="second-item">
                    <li><xsl:value-of select="concat(local-name(),' ',@second-item-id)"/></li>
                    <ul>
                        <xsl:for-each select="third-item">
                            <li><xsl:value-of select="concat(local-name(),' ',@third-item-id)"/></li>
                        </xsl:for-each>
                    </ul>
                </xsl:for-each>
            </ul>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

So here is the question: I want to process input xml with both of tempaltes (or merged one) in one step. How can i do it?

Thanks in advance.

  • 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-18T06:36:17+00:00Added an answer on June 18, 2026 at 6:36 am

    If you’re happy with just merging them together, then this should accomplish the job of both at the same time:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:output indent="yes"/>
    
      <xsl:key name="itemKey" match="item" use="@item-id"/>
      <xsl:key name="secondItemKey" match="item" use="concat(@item-id, '|', @second-item-id)"/>
    
      <xsl:template match="list">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates select="item[generate-id() = generate-id(key('itemKey', @item-id)[1])]"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="item">
        <h2>
          <xsl:value-of select="concat('item ', @item-id)"/>
        </h2>
        <ul>
          <xsl:apply-templates select="key('itemKey', @item-id)[generate-id() = generate-id(key('secondItemKey', concat(@item-id, '|', @second-item-id))[1])]" mode="evt"/>
        </ul>
      </xsl:template>
    
      <xsl:template match="item" mode="evt">
        <li>
          <xsl:value-of select="concat('second-item ', @second-item-id)"/>
        </li>
        <ul>
          <xsl:apply-templates select="key('secondItemKey', concat(@item-id, '|', @second-item-id))" mode="bus"/>
        </ul>
      </xsl:template>
    
      <xsl:template match="item" mode="bus">
        <li>
          <xsl:value-of select="concat('third-item ', @third-item-id)"/>
        </li>
      </xsl:template>
    
    </xsl:stylesheet>
    

    There is an easy enough way to have them both in one XSLT and run one after the other, but the approach I have in mind would require the use of the node-set() function which is unfortunately in a different namespace for each XSLT implementation. Which XSLT processor are you using?

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

Sidebar

Related Questions

Maybe somebody had similar problem. I need to transform using XSL the input XML
I need to transform a valid XML document to the OFX v1.0.2 format .
I need to transform one XML (say x1.xml) which is the input to my
I'm using XSLT to transform from one format of XML into another but I
I need to transform xml data into a different format. I have done very
I need to receive an editable format (xml or json or otherwise) the result
I need to read and serialize objects from and to XML, Apple's .plist format
I've a CXF RESTful service which returns both XML and Json format. I need
I'm writing a parser for an internal xml-based metadata format in python. I need
I need to format text in posts using WordPress to look like a code.

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.