RE: [xsl] identifying the specific comments associated with an element

Subject: RE: [xsl] identifying the specific comments associated with an element
From: "Josh Canfield" <Josh.Canfield@xxxxxxxxxxxx>
Date: Thu, 25 Mar 2004 20:32:34 -0800
Hi, I think this does what you want:
 
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <xsl:key name="element-to-comment" match="comment()" use="generate-id(following-sibling::*[1])"/>
  
  <xsl:template match="/dataItems">
    <xsl:copy>
      <xsl:apply-templates select="item"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="comment">
        <xsl:apply-templates select="key('element-to-comment', generate-id(.))"/>
      </xsl:attribute>
      <xsl:copy-of select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="comment()">
    <xsl:value-of select="normalize-space(.)"/>

    <!-- add a seperator if the next comment has the same next element-->
    <xsl:if test="
        generate-id(following-sibling::*[1]) = 
        generate-id(following-sibling::comment()[1]/following-sibling::*[1])">
      <xsl:text>. </xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>


Josh

-----Original Message-----
From: avi paradise [mailto:aparadise@xxxxxxxxxxxxxxx]
Sent: Thursday, March 25, 2004 6:10 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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/[@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

Current Thread