[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Keeping comments with relevant node when shorting

Subject: Re: Keeping comments with relevant node when shorting nodes
From: Mike Blake-Knox <mikebk824@xxxxxxxxx>
Date: Fri, 21 Jan 2005 09:18:04 -0500
xslt copy comments
All,

I was impressed to receive three different ways of solving my problem.

I started with Wendell's then added a sort (in-place). I then realised
that I didn't need to actually put the comments inside the output N
elements and used copy-of in the Ns/N template to generate them before
the N elements.

Thanks very much.

Here's the resulting xslt code in case it helps anyone else:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output  indent="yes" method="xml"/>

<xsl:template match="node() | @*">
   <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
   </xsl:copy>
</xsl:template>              

<xsl:key name="comments-by-owner" match="comment() | text()"
use="generate-id(following-sibling::*[1])"/>

<xsl:template match="Ns/child::text()|N/child::text()">
</xsl:template>                    

<xsl:template match="Ns">
    <xsl:copy>
        <xsl:apply-templates select="N">
       <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Ns/N">
   <xsl:copy-of select="key('comments-by-owner',generate-id())"/>
   <xsl:copy>
 <xsl:copy-of select="@*"/>
 <xsl:apply-templates select="node()"/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Mike




On Fri, 14 Jan 2005 18:13:16 -0500, Wendell Piez
<wapiez@xxxxxxxxxxxxxxxx> wrote:
> Hi Mike,
> 
> Keys are useful for this kind of grouping in XSLT 1.0.
> 
> I've never retrieved comments with a key, but that doesn't make it impossible:
> 
> <xsl:key name="comments-by-owner" match="comment()"
> use="generate-id(following-sibling::*[1])"/>
> 
> This key will retrieve all the comments that precede a given element
> (sibling), except those that also precede an earlier element.
> 
> Then when you have
> 
> <!-- comment 1 -->
> <!-- comment 2 -->
> <!-- comment 3 -->
> <node/>
> <!-- comment 4 -->
> <!-- comment 5 -->
> <node/>
> 
> <xsl:template match="node">
>   <xsl:copy>
>     <xsl:copy-of select="key('comments-by-owner',generate-id())"/
>   </xsl:copy>
> </xsl:template>
> 
> would move the comments that "belong" to each node, inside it:
> 
> <node>
>   <!-- comment 1 -->
>   <!-- comment 2 -->
>   <!-- comment 3 -->
> </node>
> <node>
>   <!-- comment 4 -->
>   <!-- comment 5 -->
> </node>
> 
> This uses the key-based idiom for positional grouping you'll find
> documented at www.jenitennison.com (which we use typically to create
> hierarchies out of flat structures -- as I've done in the example above).
> 
> XSLT 2.0 has nice grouping constructs you can use for this: look Ma, no keys.
> 
> Cheers,
> Wendell
> 
> At 03:32 PM 1/14/2005, you wrote:
> >That sorts nicely but unfortunately, we have several comments in front
> >of some node.
> >
> >I haven't been able to come up with a method of grouping the comments
> >with their associated nodes. Would you have any suggestions?
> >
> >The current xsl is:
> >
> ><?xml version="1.0" encoding="UTF-8"?>
> ><xsl:stylesheet version="1.0"
> >xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> ><xsl:output indent="yes" method="xml"/>
> >
> ><xsl:template match="node() | @*">
> ><xsl:copy>
> ><xsl:apply-templates select="@* | node()"/>
> ></xsl:copy>
> >    </xsl:template>
> >
> >  <xsl:template match="Ns">
> >      <xsl:copy>
> >          <xsl:apply-templates select="N">
> >             <xsl:sort select="@name"/>
> >        </xsl:apply-templates>
> >      </xsl:copy>
> ></xsl:template>
> >
> ><xsl:template match="N">
> ><xsl:copy-of
> >select="preceding-sibling::node()[normalize-space()][1][self::comment()]
> >"/>
> ><xsl:copy-of select="."/>
> ></xsl:template>
> >
> ></xsl:stylesheet>
> 
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
> --~--
> 
> 


-- 
Mike Blake-Knox

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.