xsl-list
[Top] [All Lists]

Re: generate-id() and setting an ID attribute uniquely in t he XML output for the same source node output twice.

2003-11-18 16:15:20
On Tuesday 18 Nov 2003 6:06 pm, 
bryan(_dot_)s(_dot_)schnabel(_at_)exgate(_dot_)tek(_dot_)com wrote:

Hi Bryan, thanks for taking the time to reply,

Dave,

One way you can set unique ids with a combination of generate-id(), and
xsl:with-param
(the rough sample XSL took a bit of time to slog through, but I modified
what you had and came up with this variation)



Yeh, sorry about that, I was trying to give a simple example of a much more 
complicated input and output document, but thanks for persevering through it.

Your solution does work for me though, I've had to modify it slightly because 
my real document allows nesting of text level elements . This means that I 
have to propagate the $dif parameter through all my text level elements 
templates, (not ideal but better than having two versions of each with a 
different mode), so using a modified version of my sample and your suggestion 
this is the result

<!-- input document 
 note in this one the span element is nested in a i element 
--->
<doc>
    <textlevel id="textlevel1">
       <i>text text text</i> <i><span id="s1">text <i>text</i> text</span></i> 
text text <b>text</b>
    </textlevel>
</doc>


<!-- xsl file 
note that //span //i //b all take the dif param and call apply-templates with 
this param
-->
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xalan="http://xml.apache.org/xalan";
  exclude-result-prefixes="xalan"
  version="1.0">
  
<xsl:template match="/">
 <resultdoc>
  <newstructure_e1>
   <xsl:apply-templates select="doc/textlevel[(_at_)id='textlevel1']">
    <xsl:with-param name="dif" select="concat(generate-id(),'-a')" />
   </xsl:apply-templates>
  </newstructure_e1>
  <newstructure_e2>
   <xsl:apply-templates select="doc/textlevel[(_at_)id='textlevel1']">
    <xsl:with-param name="dif" select="concat(generate-id(),'-b')" />
   </xsl:apply-templates>
  </newstructure_e2>
 </resultdoc>
</xsl:template>

<xsl:template match="doc/textlevel">
 <xsl:param name="dif" />
 <textlevel>
  <xsl:apply-templates >
   <xsl:with-param name="dif" select="concat(generate-id(),$dif)" />
  </xsl:apply-templates>
 </textlevel> 
</xsl:template>

<xsl:template match="//span">
 <xsl:param name="dif" />
 <NEWspan>
  <xsl:attribute name="id">
   <xsl:value-of select="$dif"/>
  </xsl:attribute>
  <xsl:apply-templates>
    <xsl:with-param name="dif" select="$dif" />
  </xsl:apply-templates>
 </NEWspan>
</xsl:template>

<xsl:template match="i">
 <xsl:param name="dif" />
 <NEWi>
  <xsl:apply-templates>
    <xsl:with-param name="dif" select="$dif" />
  </xsl:apply-templates>
 </NEWi>
</xsl:template>

<xsl:template match="b">
 <xsl:param name="dif" />
 <NEWb>
  <xsl:apply-templates>
    <xsl:with-param name="dif" select="$dif" />
  </xsl:apply-templates>
 </NEWb>
</xsl:template>
</xsl:stylesheet>



<!-- resulting output -->


<?xml version="1.0" encoding="UTF-8"?>
<resultdoc>
<newstructure_e1><textlevel>
       <NEWi>text text text</NEWi> <NEWi><NEWspan id="N10004N10000-a">text 
<NEWi>text</NEWi> text</NEWspan></NEWi> text text <NEWb>text</NEWb>
    </textlevel></newstructure_e1>
<newstructure_e2><textlevel>
       <NEWi>text text text</NEWi> <NEWi><NEWspan id="N10004N10000-b">text 
<NEWi>text</NEWi> text</NEWspan></NEWi> text text <NEWb>text</NEWb>
    </textlevel></newstructure_e2>
</resultdoc>



Thanks a lot for you help,

 
   Cheers,

             Dave.




This XSL:

<xsl:template match="/">
 <resultdoc>
  <newstructure_e1>
   <xsl:apply-templates select="doc/textlevel[(_at_)id='textlevel1']">
    <xsl:with-param name="dif" select="concat(generate-id(),'-a')" />
   </xsl:apply-templates>
  </newstructure_e1>
  <newstructure_e2>
   <xsl:apply-templates select="doc/textlevel[(_at_)id='textlevel1']">
    <xsl:with-param name="dif" select="concat(generate-id(),'-b')" />
   </xsl:apply-templates>
  </newstructure_e2>
 </resultdoc>
</xsl:template>

<xsl:template match="doc/textlevel">
 <xsl:param name="dif" />
 <textlevel>
  <xsl:apply-templates >
   <xsl:with-param name="dif" select="concat(generate-id(),$dif)" />
  </xsl:apply-templates>
 </textlevel>
</xsl:template>

<xsl:template match="//span">
 <xsl:param name="dif" />
 <NEWspan>
  <xsl:attribute name="id">
   <xsl:value-of select="$dif"/>
  </xsl:attribute>
 </NEWspan>
</xsl:template>

<xsl:template match="i">
 <NEWi>
  <xsl:value-of select="."/>
 </NEWi>
</xsl:template>

<xsl:template match="b">
 <NEWb>
  <xsl:value-of select="."/>
 </NEWb>
</xsl:template>

Produces this result (which is what I think you asked for):

<resultdoc>
   <newstructure_e1>
      <textlevel>
         <NEWi>text text text</NEWi>
         <NEWspan id="d0e2d0-a"/> text text
<NEWb>text</NEWb>
      </textlevel>
   </newstructure_e1>
   <newstructure_e2>
      <textlevel>
         <NEWi>text text text</NEWi>
         <NEWspan id="d0e2d0-b"/> text text
<NEWb>text</NEWb>
      </textlevel>
   </newstructure_e2>
</resultdoc>



Good luck,

Bryan


-- 
Dr. David Holden. (Systems Developer)
Crystallography Journals Online: <http://journals.iucr.org>

Thanks in advance:-
Please avoid sending me Word or PowerPoint attachments.
See: <http://www.fsf.org/philosophy/no-word-attachments.html>

UK Privacy (R.I.P)  : http://www.stand.org.uk/commentary.php3
Public GPG key available on request.
-------------------------------------------------------------

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>