[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: optional attributes

Subject: Re: optional attributes
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Jun 2000 15:03:36 +0100
xsl attribute optional
[I keep getting failure messages from mulberrytech.  Obviously someone
thinks I *am* taking up too much bandwidth but isn't prepared to simply
shout at me ;)  Trying a resend...]

Lisa,

>The problem is that all my images won't necessarily have all those
>attributes, and if they don't instead of outputting <img src="heart.gif"
>border="" align=""> etc as I'd hope, the stylesheet outputs <img
>src="heart.gif" border align>.

That should never happen!  XSLT Processors should always produce
well-formed XML and valid HTML.  What processor are you using?  I'd
recommend that you switch to a compliant one if that's possible.

However, as a workaround, you can test whether the attribute has a value
and only add the outputted attribute if it does:

<xsl:template match="image[@type='html']">
  <img>
    <xsl:if test="@source">
      <xsl:attribute name="src">
        <xsl:value-of select="@source"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="@border">
      <xsl:attribute name="border">
        <xsl:value-of select="@border"/>
      </xsl:attribute>
    </xsl:if>
    <!-- and so on -->
  </img>
</xsl:template>

Alternatively, if the attributes in the input have the same names as those
in the output, you can copy them directly:

<xsl:template match="image[@type='html']">
  <img>
    <xsl:if test="@source">
      <xsl:attribute name="src">
        <xsl:value-of select="@source"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="@*" />
  </img>
</xsl:template>

<xsl:template match="image/@*">
  <xsl:copy-of select="." />
</xsl:template>

Note that if you have default values for the attributes in your source,
then they will usually be added to your output: XSLT processors usually use
a parser that checks any default values defined in the DTD and therefore
don't distinguish between attributes that hold the default value and
attributes that are defined in the input with that value.

I hope that helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.