[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: how do you determine if a property exists?

Subject: RE: how do you determine if a property exists?
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Thu, 15 Apr 2004 11:49:23 -0400
xslt determine if node exists
> From: David Buddrige [mailto:dbuddrige@xxxxxxxxx] 

> --- Jarno.Elovirta@xxxxxxxxx wrote:
> > if you want to test if the attribute
> > exists, then simply
> > 
> >   <xsl:when test="@Predecessors">
> > 
> 
> For some reason, this doesn't seem to work for me.  I
> am using saxon as my xslt processor.
> 
> I have the following data:
> 
> <?xml version="1.0" encoding="iso-8859-1"?>
> <info>
> 	<mytag duration="3 days" />
> 	<mytag />
> 	<mytag duration="6 hours" />
> 	<mytag duration="33 hours" />
> 	<mytag duration="" />
> 	<mytag duration="13 hours" />
> 	<mytag duration="5 days" />
> 	<mytag duration="" />
> 	<mytag duration="3 hours" />
> 	<mytag duration="23 hours" />
> </info>
> 
> 

Here is one way to do it.  There can be a million variations on the
theme -

<xsl:template match="/info">
<results>
  <xsl:for-each select='mytag'>
    <xsl:element name='myelement'>
      <xsl:choose>
        <xsl:when test='@duration != ""'>
          <xsl:copy-of select='@duration'/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name='duration'>1</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:for-each>
</results>
</xsl:template>

Notice the sense of test.  This form picks up both a missing attribute
and an empty string value.

Cheers,

Tom P

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.