[xsl] Establishing Defaults for Omitted Elements

Subject: [xsl] Establishing Defaults for Omitted Elements
From: Dan Diebolt <dandiebolt@xxxxxxxxx>
Date: Wed, 3 Mar 2004 09:29:20 -0800 (PST)
I have an XML configuration file for which I am defining the structure of (ie I
can change it) and in relevant part currently looks like this:
 
<Items>
 <Defaults>
  <a>0</a>
  <b>1</b>
 </Defaults>
 <Item>
  <a>1</a>
 </Item>
 <Item>
  <b>2</b>
 </Item>
</Items>
 
The <Defaults> element is intended to set the default value of elements <a> and
<b> if these elements are absent in any of the subsequent <Item>'s.
 
When I process the <Item>'s and need to grab the value of <a> or <b>, I am
currently using code like this:
 

<xsl:template match="Item">
 <xsl:variable name="Defaults" select="../Defaults"/>
 
 <!-- code use when value for <a> is needed -->
 <xsl:choose>
  <xsl:when test="a">
   <xsl:value-of select="a"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$MenuDefaults/a"/>
  </xsl:otherwise>
 </xsl:choose>
 
 <!-- code use when value for <a> is needed -->
 <xsl:choose>
  <xsl:when test="b">
   <xsl:value-of select="b"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$MenuDefaults/b"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>
 
Could anyone offer a more compact way to implement a scheme like this (changing
either the XML or the XSLT)? In the actual application I have 100's of elements
like <a> and <b> and it is becoming a bit verbose to use the <xsl:choose>
element in this fashion. The solution has to work with IE6. The most important
feature I must retain is the ability to make hand editing of the XML
configuration file as simple as possible (minimal typing, no attributes etc).
There is currently no DTD or schema for the XML config file.
 
Any help would be appreciated. 
 
 



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


Current Thread