J
J Kallay
Suppose that you have a document that looks like this:
<item type="normal"/>
<item type="listitem"/>
<item type="listitem"/>
<item type="normal"/>
<item type="listitem"/>
<item type="normal"/>
and you wanted to transform it into something like this:
<item>
<list>
<item>
<item>
</list>
<item/>
<list>
<item/>
</list>
<item>
With string manipulation (regular expressions, for example), you could find
all items of type listitem that are preceded by something other than a
listitem (i.e. the first item in the list) and replace them with
<list><item/>. Similarly, you could find all listitems that are not
followed by another listitem (i.e. the list item in the list) and replace
them with <item/></list>. But how would this be done with XSLT?
The following (using 'firstlistitem' and 'lastlistitem' as shortcuts for the
query identifying items with preceding or following non-listitmes) wouldn't
work:
<xsl:template match=firstlistitem>
<list>
<xsl: value-of select=".">
</xsl:template>
<xsl:template match=lastlistitem>
<xsl: value-of select=".">
</list>
</xsl:template>
because it involves opening and closing the tag in different templates.
Instead, you'd need something like:
<xsl:template match=firstlistitem>
<list>
<xsl:for-each select="listitems between here and the next
'firstlistitem' ">
<xsl: value-of select=".">
</xsl:for-each>
</list>
</xsl:template>
It's the "listitems between here and the next 'firstlistitem' " that I'm
having trouble with.
<item type="normal"/>
<item type="listitem"/>
<item type="listitem"/>
<item type="normal"/>
<item type="listitem"/>
<item type="normal"/>
and you wanted to transform it into something like this:
<item>
<list>
<item>
<item>
</list>
<item/>
<list>
<item/>
</list>
<item>
With string manipulation (regular expressions, for example), you could find
all items of type listitem that are preceded by something other than a
listitem (i.e. the first item in the list) and replace them with
<list><item/>. Similarly, you could find all listitems that are not
followed by another listitem (i.e. the list item in the list) and replace
them with <item/></list>. But how would this be done with XSLT?
The following (using 'firstlistitem' and 'lastlistitem' as shortcuts for the
query identifying items with preceding or following non-listitmes) wouldn't
work:
<xsl:template match=firstlistitem>
<list>
<xsl: value-of select=".">
</xsl:template>
<xsl:template match=lastlistitem>
<xsl: value-of select=".">
</list>
</xsl:template>
because it involves opening and closing the tag in different templates.
Instead, you'd need something like:
<xsl:template match=firstlistitem>
<list>
<xsl:for-each select="listitems between here and the next
'firstlistitem' ">
<xsl: value-of select=".">
</xsl:for-each>
</list>
</xsl:template>
It's the "listitems between here and the next 'firstlistitem' " that I'm
having trouble with.