Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XSLForum: Main > XSL-XSD-XML and more

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-18-2008, 09:47 PM
Hvid Hat
 
Posts: n/a

Default Help needed with transition



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.

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>

To

<fieldset>
<legend>First heading</legend>
<p>Lorem</p>
<p>Ipsum</p>
</fieldset>
<fieldset>
<legend>Second heading</legend>
<p>Dolor</p>
<p>Sit</p>
</fieldset>
<fieldset>
<legend>Third heading</legend>
<p>Amet</p>
</fieldset>
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-18-2008, 11:33 PM
Hvid Hat
 
Posts: n/a

Default Re: Help needed with transition

I'm getting closer but still no cigar :-(

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
<xsl:apply-templates select="//heading" />
</xsl:template>
<xsl:template match="heading">
<fieldset>
<legend>
<xsl:value-of select="."/>
</legend>
<!-- Only text elements following above heading -->
<xsl:apply-templates select="//text" />
</fieldset>
</xsl:template>
<xsl:template match="text">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>

Where my comment is in the XSLT above I need help. I've looked at
following-sibling which could be what I need but I can't figure out how...
anyone?



On 18-11-2008 22:40:30, "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.
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <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>
>
> To
>
> <fieldset>
> <legend>First heading</legend>
> <p>Lorem</p>
> <p>Ipsum</p>
> </fieldset>
> <fieldset>
> <legend>Second heading</legend>
> <p>Dolor</p>
> <p>Sit</p>
> </fieldset>
> <fieldset>
> <legend>Third heading</legend>
> <p>Amet</p>
> </fieldset>


Reply With Quote
  #3 (permalink)  
Old 11-19-2008, 01:52 AM
David Carlisle
 
Posts: n/a

Default Re: Help needed with transition

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="*"/>
<xslutput 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="*"/>
<xslutput 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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 04:01 PM.
Style Developed by Epic Designz