This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: determining whether an XPATH points to an element or attribute


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]