Test if a node has ANY attributes

Discussion in 'XML' started by gregmcmullinjr@gmail.com, Oct 25, 2006.

  1. Guest

    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?
     
    , Oct 25, 2006
    #1
    1. Advertising

  2. roy axenov Guest

    wrote:
    > 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>

    --
    roy axenov
     
    roy axenov, Oct 25, 2006
    #2
    1. Advertising

  3. wrote:
    > 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?=, Oct 25, 2006
    #3
  4. wrote:
    > 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Oct 25, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wolf
    Replies:
    0
    Views:
    575
  2. Replies:
    0
    Views:
    1,648
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,505
    Dimitre Novatchev
    Aug 24, 2006
  4. Manoj
    Replies:
    0
    Views:
    579
    Manoj
    Nov 4, 2008
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    762
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page