E
Eric Anderson
I'm not sure if this is the correct place to post this question so
please feel free to redirect me if needed.
I am writing a XSL transform document to perform a transform on an XML
document. I want some of my XML tags to have optional attributes. If the
person does not specify the value of an attribute I will assume a
default value. What is the best way to do this in XSL. Let me give an
example to make my problem clear:
foo.xml
-------
<root>
<tag attr="value"/>
<tag attr="another_value"/>
</root>
bar.xsl
-------
<xsl:template match="/root/tag">
Attribute 'attr' is <xsl:value-of select="@attr"/>
</xsl:template>
output
------
Attribute 'attr' is value
Attribute 'attr' is another_value
Now lets make one change to the above transforms. We want the attribute
'attr' to default to the value of "default" if not specified. Now one
way to do this would be:
bar.xsl
-------
<xsl:template match="/root/tag">
<xsl:choose>
<xsl:when test="@attr = ''">
Attribute 'attr' is default
</xsl:when>
<xsl
therwise>
Attribute 'attr' is <xsl:value-of select="@attr"/>
</xsl
therwise>
<xsl:choose>
</xsl:template>
The solution above is not really scalable. What if you want to have lots
of attributes that have defaults? And what if you are doing more than
printing a sentence out? Do you really want to repeat hundreds of lines
with just a few small changes between the different blocks of code? A
better method is if we could at the top of our template simply check if
the value is set. If it is not set then we would want to be able to set
it. My first thought was to use <xsl:variable> tag like below:
<xsl:template match="/root/tag">
<xsl:choose>
<xsl:when test="@attr = ''">
<xsl:variable name="attr" select="'default'"/>
<xsl:when>
<xsl
therwise>
<xsl:variable name="attr" select="@attr"/>
<xsl
therwise>
</xsl:choose>
Attribute 'attr' is <xsl:value-of select="$attr"/>
</xsl:template>
The problem is that the variable 'attr' seems to only be defined in the
context of the conditional (at least it seems to be that way in
Mozilla's XSLT processing). Once the conditional has ended (xsl:choose)
then it is no longer defined. I tried to define an empty variable 'attr'
prior to the conditional to indicate it should be defined in the scope
of the template tag not the conditional tags, but that violate the rule
of not being allowed to change a the value of a variable (by the way,
who was the idiot that thought it was a good idea to have a tag which
defines constants and call it a variable!
).
So does anyone have any good ideas on how I would get around this? What
is the right way to do this in XSLT?
Thanks for your suggestions,
Eric
please feel free to redirect me if needed.
I am writing a XSL transform document to perform a transform on an XML
document. I want some of my XML tags to have optional attributes. If the
person does not specify the value of an attribute I will assume a
default value. What is the best way to do this in XSL. Let me give an
example to make my problem clear:
foo.xml
-------
<root>
<tag attr="value"/>
<tag attr="another_value"/>
</root>
bar.xsl
-------
<xsl:template match="/root/tag">
Attribute 'attr' is <xsl:value-of select="@attr"/>
</xsl:template>
output
------
Attribute 'attr' is value
Attribute 'attr' is another_value
Now lets make one change to the above transforms. We want the attribute
'attr' to default to the value of "default" if not specified. Now one
way to do this would be:
bar.xsl
-------
<xsl:template match="/root/tag">
<xsl:choose>
<xsl:when test="@attr = ''">
Attribute 'attr' is default
</xsl:when>
<xsl
Attribute 'attr' is <xsl:value-of select="@attr"/>
</xsl
<xsl:choose>
</xsl:template>
The solution above is not really scalable. What if you want to have lots
of attributes that have defaults? And what if you are doing more than
printing a sentence out? Do you really want to repeat hundreds of lines
with just a few small changes between the different blocks of code? A
better method is if we could at the top of our template simply check if
the value is set. If it is not set then we would want to be able to set
it. My first thought was to use <xsl:variable> tag like below:
<xsl:template match="/root/tag">
<xsl:choose>
<xsl:when test="@attr = ''">
<xsl:variable name="attr" select="'default'"/>
<xsl:when>
<xsl
<xsl:variable name="attr" select="@attr"/>
<xsl
</xsl:choose>
Attribute 'attr' is <xsl:value-of select="$attr"/>
</xsl:template>
The problem is that the variable 'attr' seems to only be defined in the
context of the conditional (at least it seems to be that way in
Mozilla's XSLT processing). Once the conditional has ended (xsl:choose)
then it is no longer defined. I tried to define an empty variable 'attr'
prior to the conditional to indicate it should be defined in the scope
of the template tag not the conditional tags, but that violate the rule
of not being allowed to change a the value of a variable (by the way,
who was the idiot that thought it was a good idea to have a tag which
defines constants and call it a variable!
So does anyone have any good ideas on how I would get around this? What
is the right way to do this in XSLT?
Thanks for your suggestions,
Eric