xslt and optional attributes

E

Edwin G. Castro

Is there a way to handle optional attributes in xslt?

I'd like to determine if an attribute exists and do some action if it
does. Otherwise, I want to take some other action or simply do
nothing.

Thanks!

--Edwin
 
R

Richard Tobin

Edwin G. Castro said:
I'd like to determine if an attribute exists and do some action if it
does. Otherwise, I want to take some other action or simply do
nothing.

Use an XPath that matches the attribute in a test, or that matches an
element that has the attribute in a template.

For example, if you have templates

<xsl:template select="foo[@bar]"> ...
and
<xsl:template select="foo"> ...

the first will match <foo>s that have a bar attribute, and the second
will match the others. Or you could use a conditional:

<xsl:choose>
<xsl:when test="@bar">
...
</xsl:when>
<xsl:eek:therwise>
...
</xsl:eek:therwise>
</xsl:choose>

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top