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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:19:29+00:00 2026-05-11T08:19:29+00:00

I have an xml document, that has a list of categories: <categories> <category id=1

  • 0

I have an xml document, that has a list of categories:

<categories>     <category id='1' parent='0'>Configurations</category>     <category id='11' parent='13'>LCD Monitor</category>     <category id='12' parent='13'>CRT Monitor</category>     <category id='13' parent='1''>Monitors</category>     <category id='123' parent='122'>Printer</category>     ... </categories> 

And a list of products:

<products>   <product>     ...    <category>12</category>     ...   </product>     ... </products> 

If product’s category is equal to 12, then it should be transformed to ‘Configurations/Monitors/CRT Monitor’ (take category 12, then it’s parent (13), etc.). If parent is 0, stop.

Is there an elegant way to do this using a XSL Transformation?

  • 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. 2026-05-11T08:19:29+00:00Added an answer on May 11, 2026 at 8:19 am

    I don’t know whether this would be considered elegant, but with this input:

    <root>     <categories>         <category id='1' parent='0'>Configurations</category>         <category id='11' parent='13'>LCD Monitor</category>         <category id='12' parent='13'>CRT Monitor</category>         <category id='13' parent='1'>Monitors</category>         <category id='123' parent='122'>Printer</category>     </categories>     <products>         <product>              <category>12</category>         </product>         <product>              <category>11</category>         </product>      </products> </root> 

    This XSLT:

    <?xml version='1.0'?>  <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  <xsl:template match='/'>   <root>   <xsl:apply-templates select='//product'/>   </root> </xsl:template>  <xsl:template match='product'>   <product>     <path>       <xsl:call-template name='catwalk'>         <xsl:with-param name='id'><xsl:value-of select='category'/>         </xsl:with-param>       </xsl:call-template>     </path>   </product> </xsl:template>  <xsl:template name='catwalk'>   <xsl:param name='id'/>   <xsl:if test='$id != '0''>     <xsl:call-template name='catwalk'>       <xsl:with-param name='id'><xsl:value-of select='//category[@id = $id]/@parent'/>       </xsl:with-param>     </xsl:call-template>     <xsl:value-of select='//category[@id = $id]'/><xsl:text>/</xsl:text>   </xsl:if> </xsl:template>  </xsl:stylesheet> 

    Will give you this output:

    <?xml version='1.0' encoding='utf-8'?>   <root>   <product>     <path>Configurations/Monitors/CRT Monitor/     </path>   </product>   <product>      <path>Configurations/Monitors/LCD Monitor/      </path>   </product>   </root> 

    The paths still have an extra trailing slash, you’d need another little bit of conditional XSLT to make the slash only get emitted when you aren’t at the first level.

    It is vital that you category hierarchy is correct, otherwise your transform can easily get into an endless loop that will only stop when it runs out of memory. If I was implementing something like this in a real system I’d be tempted to add a parameter to the catWalk template that incremented on each call and add it to the test so it stopped looping after 10 calls whether or not the parent had been found.

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

Sidebar

Related Questions

I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an XML document that has a TextBlock that contains HTML code. <TextBlock>
I have a xml document that has a record set like this. <document> <row>
I have an XML document that has end times in there, I need to
I have an XML document that has HTML tags included: <chapter> <h1>title of content</h1>
I have an XML document that has several <item> elements. Inside each, there might
I have data stored in an XML document that describes a linked list; all
I have an XML document that I'm trying to search through. The Main structure
I have an XML document that I generate from an Entity Framework object. The
I have an XML document that I load in and try to search with

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.