Test if a node has ANY attributes

G

gregmcmullinjr

I am spending alot of time on this group, lets see if we can figure
this one out:

I'm trying to determine if a node has any attributes at all. I would
like to do a shallow copy (<xsl:copy>) if it does not, and a deep copy
(<xsl:copy-of>) if it does.

I've tried something like this:

<xsl:template match="//content/*>
<xsl:choose>
<xsl:when test="exists(@*)">
...
</xsl:when>
...
</xsl:choose>
</xsl:template>

but that doesn't seem to work... Any ideas?
 
R

roy axenov

I'm trying to determine if a node has any attributes at
all. I would like to do a shallow copy (<xsl:copy>) if it
does not, and a deep copy (<xsl:copy-of>) if it does.

I've tried something like this:

<xsl:template match="//content/*>
<xsl:choose>
<xsl:when test="exists(@*)">
...
</xsl:when>
...
</xsl:choose>
</xsl:template>

but that doesn't seem to work...

Using what input XML it doesn't seem to work, and precisely
how it doesn't seem to work? Oh, and providing a sample
stylesheet that demonstrates your problem would be nice,
too.
Any ideas?

Actually, yes. Grep XSLT and XPath specs for 'exists'. It's
really enlightening, you know.

The following works:

<xsl:template match="*[@*]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*[not(@*)]">
<xsl:copy/>
</xsl:template>
 
?

=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

I am spending alot of time on this group, lets see if we can figure
this one out:

I'm trying to determine if a node has any attributes at all. I would
like to do a shallow copy (<xsl:copy>) if it does not, and a deep copy
(<xsl:copy-of>) if it does.

I've tried something like this:

<xsl:template match="//content/*>
<xsl:choose>
<xsl:when test="exists(@*)">
...
</xsl:when>
...
</xsl:choose>
</xsl:template>

but that doesn't seem to work... Any ideas?

All you need to do is test for the attribute itself.

<xsl:template match="//content/*>
<xsl:choose>
<xsl:when test="@*">
...
</xsl:when>
...
</xsl:choose>
</xsl:template>

This tells it that if ANY attribute exists, the "when" bit of code will
be executed. You can use the same logic with elements.

Regards
Jean-Francois Michaud
 
?

=?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

I am spending alot of time on this group, lets see if we can figure
this one out:

I'm trying to determine if a node has any attributes at all. I would
like to do a shallow copy (<xsl:copy>) if it does not, and a deep copy
(<xsl:copy-of>) if it does.

Also, XSLT is a functional language, you don't need to deal with the
same thought process as with procedural languages. Actually, you can't
deal with problems in the same way at all.

No need to specify weather you want a shallow copy or a deep copy.
Simply use <xsl:copy-of select=".">

Unless you need to not copy the content of the node in the case where
you don't have attributes on your node, then you can use <xsl:copy-of
select="."> in all cases.

It will simply copy the node over. If it so happens not to have
attributes, then the copy won't have attributes. If it so happens to
have attributes, they will transfer over.

Also, keep in mind that an attribute not existing vs and attribute
existing but not containing a value isn't the same thing. The cases are
treated differently.
I've tried something like this:

<xsl:template match="//content/*>
<xsl:choose>
<xsl:when test="exists(@*)">
...
</xsl:when>
...
</xsl:choose>
</xsl:template>

but that doesn't seem to work... Any ideas?

Regards
Jean-Francois Michaud
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top