xsl-list
[Top] [All Lists]

more elegant way to process element with default value?

2003-06-13 11:49:12
Hi all,
I'm trying to set a (global) variable from a global-settings/setting
element in my source XML document, but I want this variable to have a default
value if the desired <setting> element doesn't exist.  E.g. my XML source:

<llcd:llc-display>
  <llcd:global-settings>
    <llcd:setting name="number-rows">false</setting>
    ...
  </llcd:global-settings>
  ...
</llcd:llc-display>

I want to set a number-rows variable in my stylesheet to
    /*/global-settings/setting[(_at_)name='number-rows']
but I want the variable to contain 'true' if such a <setting>
node doesn't exist in the input document.

I'm doing it like this so far:
  <xsl:variable name="number-rows">
    <xsl:variable name="tmp" 
select="/*/llcd:global-settings/llcd:setting[(_at_)name = 'number-rows']"
/>
    <xsl:choose>
      <xsl:when test="$tmp"><xsl:value-of select="$tmp" /></xsl:when>
      <xsl:otherwise><xsl:value-of select="'true'" /></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

But that seems pretty verbose.  And it will be worse as I have multiple
settings variables with various names.
Is there a more elegant way?

(You may ask why I don't put the value 'true' in an attribute and
just set a default for it in the DTD.  The reason is that some settings
will have values that must be arbitrary text, and my understanding is that
certain kinds of character data cannot be stored in attribute values,
since they are CDATA not PCDATA. Am I mistaken about this?)

Thanks,
Lars


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