Looks like Josh beat me to it but here's a variation that I have used in
the past that seems to work fairly well.
This XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="dataItems"/>
</xsl:template>
<xsl:template match="dataItems">
<xsl:element name="dataItems">
<xsl:apply-templates select="item"/>
</xsl:element>
</xsl:template>
<xsl:template match="item">
<xsl:variable name="followingCommentsCountfromPrecedingItem"
select="count(preceding-sibling::item[1]/following-sibling::comment())"/
<xsl:variable name="followingCommentsCountfromCurrentItem"
select="count(following-sibling::comment())"/>
<xsl:variable name="totalPrecedingCommentsAssociatedWithThisItem">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="count(preceding-sibling::comment())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$followingCommentsCountfromPrecedingItem
- $followingCommentsCountfromCurrentItem"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="item">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:attribute name="comment">
<xsl:for-each select="preceding-sibling::comment()[position()
<= $totalPrecedingCommentsAssociatedWithThisItem]">
<xsl:value-of select="concat('COMMENT: ', normalize-space(.),
' ')"/>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Gives you this output:
<?xml version="1.0" encoding="UTF-8"?>
<dataItems>
<item name="itemName1" comment="COMMENT: this is a comment about the
first item ">I1</item>
<item name="itemName2" comment="COMMENT: this is a comment about the
second item COMMENT: this is a follow on comment for 2nd item
">I2</item>
<item name="itemName3" comment="COMMENT: this is a comment about the
third item COMMENT: 3rd item also has an additional comment line
">I3</item>
<item name="itemName4" comment="COMMENT: this is a comment about the
fourth item ">I4</item>
</dataItems>
Best of luck!
<M:D/>
-----Original Message-----
From: avi paradise [mailto:aparadise(_at_)eyecatching(_dot_)com]
Sent: Thursday, March 25, 2004 7:10 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] identifying the specific comments associated with an
element
I would like to process data that is in the following format.
<dataItems>
<!-- this is a comment about the first item -->
<item name="itemName1">I1</item>
<!-- this is a comment about the second item -->
<!-- this is a follow on comment for 2nd item -->
<item name="itemName2">I2</item>
<!-- this is a comment about the third item -->
<!-- 3rd item also has an additional comment line -->
<item name="itemName3">I3</item>
<!-- this is a comment about the fourth item -->
<item name="itemName4">I4</item>
</dataItems>
In particular I would like to be able to process one item at a time as
is customarily done with <xsl:apply-templates select="/dataItems/item"
/>
What I don't understand how to do is associate the comments with the
appropriate data items.
For instance, how would I process this data to convert the comments into
attributes as follows:
<dataItems>
<item name="itemName1" comment="this is a comment about the first
item">I1</item>
<item name="itemName2" comment="this is a comment about the second
item. this is a follow on comment for 2nd item">I2</item>
<item name="itemName3" comment="this is a comment about the third
item. 3rd item also has an additional comment line">I3</item>
<item name="itemName4" comment="this is a comment about the fourth
item">I4</item>
</dataItems>
Or how would I select the item whose name is "itemName3" along with it's
comments? i.e call
<xsl:apply-templates select="/dataItems/item/[(_at_)name = 'itemName3']" />
and within the item template grab the comments
<!-- this is a comment about the third item -->
<!-- 3rd item also has an additional comment line -->
Thanks,
ap
--+------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
You are subscribed as: m(_dot_)david(_at_)mdptws(_dot_)com
To unsubscribe, go to:
http://lists.mulberrytech.com/unsub.php/xsl-list/m(_dot_)david(_at_)mdptws(_dot_)com
or e-mail:
<mailto:xsl-list-unsubscribe-m(_dot_)david=mdptws(_dot_)com(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--