Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Identity transform on node-set loaded with document()

From: Abel Braaksma <abel.online@--------->
To:
Date: 8/2/2007 9:20:00 AM
Matt Poff wrote:
Hi,



I've been away from XSLT a while and am stumbling on how to implement 
the following:

Welcome back to XSLT ;)




My transform imports, using document(), an HTML snippet contain a 
populated <head></head> tag. I want to copy this into one of several  
result trees I am outputting  as is *except*  the <title/> tag needs 
to be populated with a  value.



Initially I created a named template and sent the loaded node-set to 
it, then realised I probably needed an identity transform but all of 
the identity transform examples I've found seem to be set-up to 
operate on the master document only. What's the best way to carry out 
this task? Can I do an identity transform with a named template?



I'm sure there's a fairly simple solution but it's passing me by.




If you mean that your matching templates are currently triggered by both 
the principal source document and the sources requested via 
document('xxx') you can use a mode in both the templates and the 
apply-template that have to deal with document('xxx'). In XSLT 2.0 you 
can combine rules for several modes (if they are they same) by using a 
combination of mode="#all" in the matching template, mode="#current" in 
the contained apply-templates and your new mode="newtitle" in your 
specific apply-template (where you call your document). I.e., the 
following code shares the copy template for both your principal source 
and your secondary source document:



<xsl:template match="/">
  <xsl:apply-templates select="document('xyz')" mode="newtitle" />
  <xsl:apply-templates select="*" />
</xsl:template>

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

<xsl:template match="title" mode="newtitle">
    <title>a new title</title>
</xsl:template>


If you are stuck with XSLT 1.0 you cannot share the templates this way 
and you need to create one copy template for each mode.



Other options for distinguishing between the two node trees can be based 
on unique properties, i.e., the namespace or a specific pattern, but 
these methods are more of a hassle, usually. And yes, you can also do it 
with call-template, for-each etc, but then you need much more 
instructions and logic. Let XSLT do the job for you with 
apply-templates/matching templates is often the easiest approach.



Cheers,
-- Abel Braaksma


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent