Re: [xsl] determining whether an XPATH points to an element or attribute

Subject: Re: [xsl] determining whether an XPATH points to an element or attribute
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Jan 2002 10:33:36 +0000
Hi Edward,

> I am generating a XSLT stylesheet from an XML file that looks like this.
>
> <path>//tagname</path>
> <path>//@attributename</path>
>
> some point to attributes and some pointing to elements.

And you want to generate different kinds of content for the templates,
depending on whether the path is for an element or attribute.

Really it depends on how complicated your patterns are. If you just
have ones in the form above, then you can test whether it matches an
element or attribute by seeing whether the first three characters are
'//@' or not:

  <xsl:choose>
    <xsl:when test="starts-with(., '//@')">
      ... attribute ...
    </xsl:when>
    <xsl:otherwise>
      ... element ...
    </xsl:otherwise>
  </xsl:choose>

If they're a little bit more complicated, then you might be able to
get away with just testing whether the patterns contain @ or not:

  <xsl:choose>
    <xsl:when test="contains(., '@')">
      ... attribute ...
    </xsl:when>
    <xsl:otherwise>
      ... element ...
    </xsl:otherwise>
  </xsl:choose>

But if they're really fairly complicated, with predicates and things,
then you need to write a XPath parser to work out whether they match
elements or attributes. And that's very difficult.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread