xsl-list
[Top] [All Lists]

RE: how do you determine if a property exists?

2004-04-16 00:15:29
Your template rule:

<xsl:template match="mytag">
                <xsl:element name="myelement">
                        <xsl:attribute name="time">
                                <xsl:choose>
                                        <xsl:when
test="@duration=''">1</xsl:when>
                                        <xsl:when
test="not(@duration)">1</xsl:when>
                                        <xsl:otherwise><xsl:value-of
select="@duration"/></xsl:otherwise>
                                </xsl:choose>
                        </xsl:attribute>
                </xsl:element>
        </xsl:template>

can be written in XSLT 2.0 as

<xsl:template match="mytag">
  <myelement time="{if (@duration != '') then @duration else 1}"/>
</xsl:template>

or in 1.0 as

<xsl:template match="mytag">
  <myelement time="1">
    <xsl:if test="@duration != ''">
        <xsl:attribute name="time">
        <xsl:value-of select="@duration"/>
      </xsl:attribute>
    </xsl:if>
  </myelement>
</xsl:template>

Michael Kay

 

-----Original Message-----
From: David Buddrige [mailto:dbuddrige(_at_)yahoo(_dot_)com] 
Sent: 16 April 2004 01:21
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] how do you determine if a property exists?

thanks everyone.  The solution I settled on was:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
      <xsl:template match="/">
              <myoutput>
                      <xsl:apply-templates/>
              </myoutput>
      </xsl:template>
      <xsl:template match="mytag">
              <xsl:element name="myelement">
                      <xsl:attribute name="time">
                              <xsl:choose>
                                      <xsl:when 
test="@duration=''">1</xsl:when>
                                      <xsl:when 
test="not(@duration)">1</xsl:when>
                                      <xsl:otherwise><xsl:value-of
select="@duration"/></xsl:otherwise>
                              </xsl:choose>
                      </xsl:attribute>
              </xsl:element>
      </xsl:template>
</xsl:stylesheet>

which given the input 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>


produces:


<?xml version="1.0" encoding="UTF-8"?><myoutput>
      <myelement time="3 days"/>
      <myelement time="1"/>
      <myelement time="6 hours"/>
      <myelement time="33 hours"/>
      <myelement time="1"/>
      <myelement time="13 hours"/>
      <myelement time="5 days"/>
      <myelement time="1"/>
      <myelement time="3 hours"/>
      <myelement time="23 hours"/>
</myoutput>

thanks again! 8-)

David.


--- Wendell Piez <wapiez(_at_)mulberrytech(_dot_)com> wrote:
At 11:49 AM 4/15/2004, 'twas written:
<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>

Or:

<xsl:template match="/info">
   <results>
     <xsl:for-each select='mytag'>
       <myelement duration='1'>
         <xsl:copy-of
select='@duration[normalize-space()]'/>
       </myelement>
     </xsl:for-each>
   </results>
</xsl:template>

Take your pick: clarity vs. succinctness.

Cheers,
Wendell




======================================================================
Wendell Piez                           
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.               
http://www.mulberrytech.com
17 West Jefferson Street                    Direct
Phone: 301/315-9635
Suite 207                                         
Phone: 301/315-9631
Rockville, MD  20850                                
Fax: 301/315-8285

----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing
in SGML and XML

======================================================================



--+------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--




      
              
__________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--