Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Help needed with transition

From: David Carlisle <david-news@---------.-----.--.-->
To: NULL
Date: 11/19/2008 12:21:00 AM
Hvid Hat wrote:
> Hi
> 
> Can anyone help me with the following transition? My problem is how to create
> a fieldset each time I run into a heading and then include the following text
> elements within the fieldset.
> 


Don't think of it that way it will lead to pain, xslt doesn't assume a 
processing order so it is hard to say stop one fieldset and start 
another when you see a heading. Instead give a global description: you 
want to group all the fields in order, with groups starting at each 
heading. the XSLT2 version follws immediately from this, and teh XSLT 1 
version is a standard rewriting of that to the muenchian grouping idiom, 
which looks a bit srange but google will show dozens of examples.



<x>
<field>
   <heading>First heading</heading>
</field>
<field>
   <text>Lorem</text>
</field>
<field>
   <text>Ipsum</text>
</field>
<field>
   <heading>Second heading</heading>
</field>
<field>
   <text>Dolor</text>
</field>
<field>
   <text>Sit</text>
</field>
<field>
   <heading>Third heading</heading>
</field>
<field>
   <text>Amet</text>
</field>
</x>


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

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:template match="x">
   <xsl:for-each-group select="field" group-starting-with="field[heading]">
     <fieldset>
       <xsl:apply-templates select="current-group()"/>
     </fieldset>
   </xsl:for-each-group>
</xsl:template>

<xsl:template match="heading">
   <legend><xsl:value-of select="."/></legend>
</xsl:template>

<xsl:template match="text">
   <p><xsl:value-of select="."/></p>
</xsl:template>

</xsl:stylesheet>



or if you are still using xslt1:


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

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="f" match="text" 
use="(../preceding-sibling::field/heading)[last()]"/>

<xsl:template match="x">
   <xsl:for-each select="field/heading">
     <fieldset>
       <xsl:apply-templates select=".|key('f',.)"/>
     </fieldset>
   </xsl:for-each>
</xsl:template>

<xsl:template match="heading">
   <legend><xsl:value-of select="."/></legend>
</xsl:template>

<xsl:template match="text">
   <p><xsl:value-of select="."/></p>
</xsl:template>

</xsl:stylesheet>




> <?xml version="1.0" encoding="ISO-8859-1"?>
> <field>
>   <heading>First heading</heading>
> </field>
> <field>

That input isn't well formed so I put in a surrounding x element

>   


-- 
http://dpcarlisle.blogspot.com


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