[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: Is self() an attribute or element?


Subject: Re: Is self() an attribute or element?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 Oct 1999 16:53:08 -0400

At 99/10/15 14:58 -0400, Clark C. Evans wrote:
I have a template that is called
to do default processing for a
particular node.  I'd like to
be able to tell if the node is
an attribute or element node.
Any ideas?

This turns out to be not straightforward in the general case ... though the need to do such a test in the general case is not typical for formatting, so I'm not disappointed at the design of XSLT.


When doing transformation for the purposes of formatting, I would posit there is very rarely a need to do this kind of test. When would formatting content require similar processing on *every* kind of node?

See my prior post for a simple true/false of "am I an element?" which would handle most of the needs.

Remember from the XSLT abstract: "XSLT is not intended as a completely general-purpose XML transformation language." So not supporting such a function is not to the detriment of the objective as stated.

The one kind of function that can be executed on any node is the generate-id() function, so I would approach it as follows.

Can anyone think of a more concise approach?

............... Ken



T:\clark>type partlist.xml
<?xml version="1.0"?>
<!--start-->
<part-list><part-name part-nbr="A123">bolt</part-name>
<part-name part-nbr="B456">washer</part-name><warning type="ignore"/>
<!--end of parts--><?cursor blinking?>
</part-list>
T:\clark>type shownode.xsl
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nl "&#xd;&#xa;">]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="text()|@*"/>

<xsl:template match="/|*|@*|processing-instruction()|
                     comment()|text()">
  <xsl:variable name="this-node" select="generate-id(.)"/>
  <xsl:variable name="this-type">
    <xsl:for-each select="../*">
      <xsl:if test="generate-id(.)=$this-node">Element</xsl:if>
    </xsl:for-each>
    <xsl:for-each select="../@*">
      <xsl:if test="generate-id(.)=$this-node">Attribute</xsl:if>
    </xsl:for-each>
    <xsl:for-each select="../processing-instruction()">
      <xsl:if test="generate-id(.)=$this-node">PI</xsl:if>
    </xsl:for-each>
    <xsl:for-each select="../comment()">
      <xsl:if test="generate-id(.)=$this-node">Comment</xsl:if>
    </xsl:for-each>
    <xsl:for-each select="../text()">
      <xsl:if test="generate-id(.)=$this-node">Text</xsl:if>
    </xsl:for-each>
    <xsl:if test="not(..)">
      <xsl:text>Root</xsl:text>
    </xsl:if>
  </xsl:variable>
  <xsl:text>&nl;Node </xsl:text><xsl:value-of select="name(.)"/>
  <xsl:text> is a node of type: </xsl:text>
  <xsl:value-of select="$this-type"/>
  <xsl:if test="string($this-type)='Attribute'">
    <xsl:text> (BINGO!! Found an attribute)</xsl:text>
  </xsl:if>
  <xsl:apply-templates select="*|@*|processing-instruction()|
                               comment()|text()"/>
</xsl:template>

</xsl:stylesheet>
T:\clark>xt partlist.xml shownode.xsl

Node  is a node of type: Root
Node  is a node of type: Comment
Node part-list is a node of type: Element
Node part-name is a node of type: Element
Node part-nbr is a node of type: Attribute (BINGO!! Found an attribute)
Node  is a node of type: Text
Node  is a node of type: Text
Node part-name is a node of type: Element
Node part-nbr is a node of type: Attribute (BINGO!! Found an attribute)
Node  is a node of type: Text
Node warning is a node of type: Element
Node type is a node of type: Attribute (BINGO!! Found an attribute)
Node  is a node of type: Text
Node  is a node of type: Comment
Node cursor is a node of type: PI
Node  is a node of type: Text
T:\clark>



--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Web site: XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-02-0
Next instructor-led training:  1999-11-08, 1999-11-09, 1999-12-05/06,
                             1999-12-07, 2000-02-27/28, 2000-05-11/12


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




Current Thread
Keywords