Altova Mailing List Archives


Re: [xsl] Creating Nested Structure Based on Attributes

From: Jeff Sese <jsese@------------>
To:
Date: 3/20/2007 8:20:00 AM
Thanks, Abel and David.



Both works well, but i think i'll go with abel's solution because i'm 
using xslt 2.0.



--
*Jeff Sese*

Abel Braaksma wrote:
Jeff Sese wrote:

<inline font-style="italic" font-variant="small-caps" 
font-weight="bold" vertical-alignment="superscript">text</inline>




Hi Jeff,



David Carlisle already showed you how to do it in XSLT 1.0 with mode 
switching. I don't know if you have the possibility to use XSLT 2.0, 
but here's a solution that does the same in XSLT 2.0, with the help of 
a little function that turns any range of attributes into a 
hierarchical node list, making it easier to use with matching 
templates. It's perhaps a bit non-conventional, but it keeps your code 
pretty tight.



Output from your source is equal to the requested output.



Happy coding!



-- Abel Braaksma
  http://abelleba.metacarpus.com


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

   <xsl:output indent="yes" />



   <xsl:template match="inline">

       <xsl:copy>

           <xsl:apply-templates 
select="f:attr-to-nodes-hier(attribute::*, text())" />

       </xsl:copy>

   </xsl:template>



   <!-- result from f:attr-to-nodes-hier -->

   <xsl:template match="attr">

       <emphasis format="{@value}">

           <xsl:apply-templates select="node()" />

       </emphasis>

   </xsl:template>

     <!-- result from f:attr-to-nodes-hier -->

   <xsl:template match="attr[@name = 'vertical-alignment']">

       <superscript>

           <xsl:apply-templates select="node()" />

       </superscript>

   </xsl:template>

     <xsl:function name="f:attr-to-nodes-hier" as="node()*">

       <xsl:param name="attributes" as="attribute()*" />

       <xsl:param name="text" as="text()" />

       <xsl:if test="$attributes">

           <attr name="{$attributes[1]/name()}" value="{$attributes[1]}">

               <xsl:copy-of 
select="f:attr-to-nodes-hier($attributes[position() > 1], $text)" />

           </attr>

       </xsl:if>

       <xsl:if test="empty($attributes)">

           <xsl:sequence select="$text" />

       </xsl:if>              </xsl:function>

</xsl:stylesheet>

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.