xsl-list
[Top] [All Lists]

Re: Reassinging attribute values

2006-01-11 06:32:53

So basically I am looking for a way to change an attribute value in
the tree while I am processing the document. Is there any way to do
this? 

XSLT never changes its input tree, the model is that the input is
read-only and you generate a new document.

The trick for arranging that generated ids and references to the
generated id use the same generated value is to generate the value at
the same place.

So suppose your source has 
<foo id="old"/>
...
<reference refid="old"/>

first have a template that makes new ids from old, assuming it is called
at the place that is referenced (<foo> here).

<xsl:template name="idmunger">
  <xsl:value-of select="generate-id()"/>
  <xsl:value-of select="count(ancestor::*)"/>
  <xsl:value-of select="substring(@id,1,2)"/>
</xsl:template>

(or whatever function you need) then call it twice:


<xsl:template match="foo">
 <newfoo>
  <xsl:attribute name="id">
    <xsl:call-template name="idmunger"/>
  </xsl:attribute>
  <xsl;apply-templates/>
 </newfoo>
</xsl:template>

and

<xsl:template match="reference">
<newreference>
 <xsl:for-each select="key('id',@idref)[1]">
  <xsl:attribute name="idref">
    <xsl:call-template name="idmunger"/>
  </xsl:attribute>
 </xsl:for-each>
</newreference>
</xsl:template>

together with

<xsl:key name="id" match="*" use="@id"/>


so this generates

<newfoo id="asjafha5ol"/> .... <newrefererence refid="asjafha5ol"/> with
the same generated value in both places as it is generated on the same
(foo) node in each case.

David

________________________________________________________________________
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
________________________________________________________________________

--~------------------------------------------------------------------
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(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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