This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: If vs. apply-templates for optional attributes
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Hunsberger, Peter" <Peter dot Hunsberger at stjude dot org>
- Cc: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Fri, 7 Jun 2002 16:36:08 +0100
- Subject: Re: [xsl] If vs. apply-templates for optional attributes
- Organization: Jeni Tennison Consulting Ltd
- References: <601F6322AD71D5118D6C0003472515290660CEAD@sjmemexc1.stjude.org>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Peter,
>> <xsl:template match="xxx">
>> <xxx attr="default">
>> <xsl:copy-of select="@*"/>
>> ..
>> </xxx>
>>
>> which will mean that xxx has the attributes from the source, plus
>> possibly attr="default" if teh source did not have that attribute.
>
> Yes, but that doesn't help if the source also has specified the
> attribute. In that case I'd end up with it defined twice...
No; an element can only have one copy of an attribute (XSLT never
generates non-well-formed XML unless you start using
disable-output-escaping), and the way XSLT works is that if you add an
attribute to an element, but the attribute is already present, the new
attribute gets added. Here, the attr attribute is added through the
literal attribute on the xxx element, and then (if present in the
source) is overridden by the attribute from the source. So what David
suggested would work very well -- you get the default value if the
attr attribute hasn't been specified, and the value of the attr
attribute if it has.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list