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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:59:47+00:00 2026-05-16T23:59:47+00:00

I am using XSLT to recurse some XML and then apply some HTML to

  • 0

I am using XSLT to recurse some XML and then apply some HTML to the output. It will recurse the data, but it duplicates the parent item description and I am not sure why? I am sure it is right in front of my face, but I don’t see it. It is inserting right after the <ul> tag when it goes to the next level in the XML.

XML Example:

<root>
    <filters>
        <filter ID="My Test">
            <item id="1">
                <description>MyTest Descrip</description>
                <item id="1">
                    <description>Sub Level - 1</description>
                </item>
                <item id="2">
                    <description>Sub Level - 2</description>
                </item>
                <item id="3">
                    <description>Sub Level - 3</description>
                <item id="4">
                <description>Sub Level 2 - 1</description>
                </item>
                    <item id="5">
                    <description>Sub Level 2 - 2</description>
                    </item>
                </item>
            </item>
        </filter>
    </filters>
</root>

XSLT Example:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="filter">
    <xsl:variable name="dataID" select="@ID"/>
        <ul class="searchdata">
            <xsl:apply-templates select="item"/>
        </ul>
    </xsl:template>
    <xsl:template match="item">
    <li>
        <xsl:variable name="searchID" select="@id"/>
        <input id="{$searchID}" type="checkbox"/>
        <label for="{$searchID}">
            <xsl:value-of select="description"/>
        </label>
        <xsl:if test="item">
        <ul>
            <xsl:apply-templates />
        </ul>
        </xsl:if>
    </li>
</xsl:template>
</xsl:stylesheet>

HTML Output:

<?xml version="1.0" encoding="utf-8"?>

    <ul class="searchdata"><li><input id="1" type="checkbox" /><label for="1">MyTest Descrip</label><ul>
    MyTest Descrip
     <li><input id="1" type="checkbox" /><label for="1">Sub Level - 1</label></li>
     <li><input id="2" type="checkbox" /><label for="2">Sub Level - 2</label></li>
          <li><input id="3" type="checkbox" /><label for="3">Sub Level - 3</label><ul>
              Sub Level - 3
              <li><input id="4" type="checkbox" /><label for="4">Sub Level 2 - 1</label></li>

              <li><input id="5" type="checkbox" /><label for="5">Sub Level 2 - 2</label></li>
          </ul></li>
     </ul></li></ul>

Any suggestions would be greatly appreciated.

Thanks.

  • 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-16T23:59:48+00:00Added an answer on May 16, 2026 at 11:59 pm

    The problem is that you are not taking note about built-in rules, built-in rules for text node and elements in particular.

    <xsl:template match="*|/">
      <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="text()|@*">
      <xsl:value-of select="."/>
    </xsl:template>
    

    So, you need to add this strip-text-nodes rule:

    <xsl:template match="text()"/>
    

    And then your output will be:

    <ul class="searchdata">
        <li>
            <input id="1" type="checkbox" />
            <label for="1">MyTest Descrip</label>
            <ul>
                <li>
                    <input id="1" type="checkbox" />
                    <label for="1">Sub Level - 1</label>
                </li>
                <li>
                    <input id="2" type="checkbox" />
                    <label for="2">Sub Level - 2</label>
                </li>
                <li>
                    <input id="3" type="checkbox" />
                    <label for="3">Sub Level - 3</label>
                    <ul>
                        <li>
                            <input id="4" type="checkbox" />
                            <label for="4">Sub Level 2 - 1</label>
                        </li>
                        <li>
                            <input id="5" type="checkbox" />
                            <label for="5">Sub Level 2 - 2</label>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    

    Also, this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="item"/>
        <xsl:template match="item[1]">
            <ul class="searchdata">
                <xsl:apply-templates select="../item" mode="li"/>
            </ul>
        </xsl:template>
        <xsl:template match="item" mode="li">
            <li>
                <input id="{@id}" type="checkbox"/>
                <xsl:apply-templates/>
            </li>
        </xsl:template>
        <xsl:template match="description">
            <label for="{../@id}">
                <xsl:value-of select="."/>
            </label>
        </xsl:template>
    </xsl:stylesheet>
    

    Note: This has a rule matching description element explicitly, wich has more priority than built-in template rule for elements (apply templates to child nodes).

    And last, this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="node()">
            <xsl:apply-templates select="node()[1]|following-sibling::node()[1]"/>
        </xsl:template>
        <xsl:template match="item[1]">
            <ul class="searchdata">
                <xsl:call-template name="item"/>
            </ul>
        </xsl:template>
        <xsl:template match="item" name="item">
            <li>
                <input id="{@id}" type="checkbox"/>
                <xsl:apply-templates select="node()[1]"/>
            </li>
            <xsl:apply-templates select="following-sibling::node()[1]"/>
        </xsl:template>
        <xsl:template match="description">
            <label for="{../@id}">
                <xsl:value-of select="."/>
            </label>
            <xsl:apply-templates select="following-sibling::node()[1]"/>
        </xsl:template>
    </xsl:stylesheet>
    

    Note: This use sequencial (“most fine grained traversal”) instead of recursive template applying.

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

Sidebar

Related Questions

I'd like to output html controls using xslt, but I need to be able
I want to apply an XSLT Stylesheet to an XML Document using C# and
I am using XSLT to generate my HTML. I am having below xml and
I am using xslt to transform an xml document to html for use in
I'm using XSLT 1.0 to transform some XML. I'm not quite sure the best
I am using XSLT to transform a XML file in to a HTML file.
I am using XSLT to convert XML to HTML. I am having trouble figuring
Is it possible to merge elements using XSLT. If I have the following XML
Using XSLT 1.0 - I have the following xml and I an trying to
When using XSLT how do I apply a class to an element which already

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.