|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Grouping Nodes ?
Ciaran Byrne wrote:
> I'm attempting to transform the following
>
> <go/><postfield id="A"/><refresh/><postfield id="B"/>
>
> into....
>
> <go><postfield id="A"/></go><refresh><postfield id="B"/></refresh>
>
> i.e. Wrap the 'postfield' element in a 'go' until a
> sibling is encountered which isn't a 'postfield'.
> In this case it's 'refresh'. The same rules apply for 'refresh'.
>
> I won't include my code since it's useless ;) but any help is appreicated.
Hi Ciaran,
Here's the solution:
xml source:
----------
<top>
<go />
<postfield id="A" />
<postfield id="A" />
<postfield id="A" />
<refresh />
<postfield id="B" />
<postfield id="B" />
</top>
stylesheet:
----------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*[name()='go' or name()='refresh']">
<xsl:choose>
<xsl:when test="following-sibling::*[1][name()='postfield']">
<xsl:variable name="firstNonWrappable"
select="following-sibling::*
[name() != 'postfield'][1]"/>
<xsl:variable name="firstNonWrappablePosition">
<xsl:choose>
<xsl:when test="$firstNonWrappable">
<xsl:value-of
select="count($firstNonWrappable/preceding-sibling::*)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(../*) + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="following-sibling::*
[count(preceding-sibling::*)
<
$firstNonWrappablePosition
]"
mode="wrapped"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="postfield" mode="wrapped">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Result:
------
<go>
<postfield id="A" />
<postfield id="A" />
<postfield id="A" />
</go>
<refresh>
<postfield id="B" />
<postfield id="B" />
</refresh>
Hope this helped.
Cheers,
Dimitre Novatchev.
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








