Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Removing hierarchy

From: Martin Honnen <Martin.Honnen@---.-->
To: xsl-list@-----.------------.---
Date: 10/1/2009 11:51:00 AM
rowan@s... wrote:
> I've got an input file like this:
> <items>
>   <item>
>     <a>abc</a>
>     <b>def</b>
>     <item>
>       <a>ghi</a>
>       <b>jkl</b>
>     </item>
>     <item>
>       <a>mno</a>
>       <b>pqr</b>
>       <item>
>         <a>stu</a>
>         <b>vwx</b>
>       </item>
>     </item>
>   </item
> </items
> 
> The items can be nested to any depth.
> 
> I want to convert this to:
> 
> <items>
>   <item>
>     <level>0</level>
>     <a>abc</a>
>     <b>def</b>
>   </item
>   <item>
>     <level>1</level>
>     <a>ghi</a>
>     <b>jkl</b>
>   </item>
>   <item>
>     <level>1</level>
>     <a>mno</a>
>     <b>pqr</b>
>   </item
>   <item>
>     <level>2</level>
>     <a>stu</a>
>     <b>vwx</b>
>   </item>
> </items
> 
> In other words I want to remove the hierarchy of items and replace it with
> a 'level' element.
> 
> What's the best way of doing this?

Here is an XSLT 1.0 way:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:output indent="yes"/>

   <xsl:template match="items">
     <xsl:copy>
       <xsl:apply-templates select="descendant::item"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="item">
     <xsl:copy>
       <level><xsl:value-of select="count(ancestor::item)"/></level>
       <xsl:copy-of select="*[not(self::item)]"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@l...>
--~--



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent