[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Keeping comments with relevant node when shorting nodes


Subject: RE: [xsl] Keeping comments with relevant node when shorting nodes
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 13 Jan 2005 23:21:51 -0000

Try:

<xsl:apply-templates select="N">
  <xsl:sort select="@name"/>
</xsl:apply-templates>

<xsl:template match="N">
  <xsl:copy-of select="preceding-sibling::comment()[1]
  <xsl:copy-of select="."/>
</xsl:template>

This assumes that every N element has an associated comment.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Mike Blake-Knox [mailto:mikebk824@xxxxxxxxx] 
> Sent: 13 January 2005 22:32
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Keeping comments with relevant node when shorting nodes
> 
> I have several XML files like the following:
> 
> <!-- CNs-->
> <Ns>
>     <!--CN1-->
>     <N name="N1">
>         <Ms>
>             <!--C1N3-->
>             <M name="1N3"/>
>             <!--C1N1-->
>             <M name="1N1"/>
>             <!--C1N2-->
>             <M name="1N2"/>
>         </Ms>
>     </N>
>     <!--CN2-->
>      <N name="N2">
>          <Ms>
>              <!--C2N2-->
>              <M name="2N2"/>
>              <!--C2N3-->
>              <M name="2N3"/>
>              <!--C2N1-->
>              <M name="2N1"/>
>          </Ms>
>      </N>
> </Ns>
> 
> 
> I'd like to sort them while keeping comments with the nodes they refer
> to (e.g., keep <!--CN2--> with <M name="2N2"/>).
> 
> I've tried the following xslt:
> 
> <?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="comment()| N" >
>           <xsl:sort select="@name"/>
>         </xsl:apply-templates>
>     </xsl:copy>
> </xsl:template>
> </xsl:stylesheet>
> 
> It sorts fine but puts the comments  for all the N nodes together at
> the top of the Ns element. I've played around with trying to group the
> comments and N nodes using preceding-sibling but have not been
> succesful.
> 
> Any ideas?
> 
> Thanks
> 
> -- 
> Mike Blake-Knox


Current Thread
Keywords
xml