Re: [xsl] Problem with generating references.

Subject: Re: [xsl] Problem with generating references.
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 5 Jul 2005 13:56:56 +0100
When gereating references it's usually easiest to make sure that current
node is the same (referenced) node, both at the point where you make the
reference, and at the point where you make the anchor, That way it is
easy to make sure that you generate the same ID, eitherusing numbering
or, as here, using generate-id.

something like



<A1>
         <B1/>
         <B2>
                  <C1></C1>
                  <C2>
                           <D1/>
                  </C2>
         </B2>
</A1>





<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">



<xsl:output indent="yes"/>

<xsl:key name="d" match="*" use="count(ancestor::*)"/>

<xsl:template match="/">
<xsl:for-each select="//*[generate-id()=generate-id(key('d',count(ancestor::*)))]">
<xsl:variable name="d" select="count(ancestor::*)"/>
<xsl:variable name="n">
 <xsl:number value="$d+1" format="A"/>
</xsl:variable>
<xsl:element name="{$n}-Group">
 <xsl:for-each select="key('d',$d)">
  <xsl:copy>
   <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
  <xsl:for-each select="*">
  <reference id="{generate-id()}"/>
  </xsl:for-each>
   </xsl:copy>
 </xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


$ saxon ids.xml ids.xsl
<?xml version="1.0" encoding="utf-8"?>
<A-Group>
   <A1 id="d0e1">
      <reference id="d0e3"/>
      <reference id="d0e5"/>
   </A1>
</A-Group>
<B-Group>
   <B1 id="d0e3"/>
   <B2 id="d0e5">
      <reference id="d0e7"/>
      <reference id="d0e9"/>
   </B2>
</B-Group>
<C-Group>
   <C1 id="d0e7"/>
   <C2 id="d0e9">
      <reference id="d0e11"/>
   </C2>
</C-Group>
<D-Group>
   <D1 id="d0e11"/>
</D-Group>



________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread