This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: optional attributes


[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@epistemics.co.uk



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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]