[xsl] selecting elements depending on existence of attributes

Subject: [xsl] selecting elements depending on existence of attributes
From: Sebastian Schirmer <schirmer@xxxxxxxxxxxxx>
Date: Thu, 3 May 2001 17:03:31 +0200
Hello!

I have a XML file that looks like this:


--- snippet xml_to_jsp_include_02.xml ---
<properties>
	<env-entry>
		<env-entry-name cols="2">Name1</env-entry-name>
		<env-entry-type>java.lang.String</env-entry-type>
		<env-entry-value>
			some Content 1
		</env-entry-value>
	</env-entry>
	<env-entry>
		<env-entry-name cols="3">Name1</env-entry-name>
		<env-entry-type>java.lang.String</env-entry-type>
		<env-entry-value>
			some Content 2
		</env-entry-value>
	</env-entry>
	<env-entry>
		<env-entry-name>Name3</env-entry-name>
		<env-entry-type>java.lang.String</env-entry-type>
		<env-entry-value>
			some Content 3
		</env-entry-value>
	</env-entry>	
</properties>
--- snippet ---


There are some env-entry-name elements that have an attribute cols, some do
not. If they have such an attribute, there could exist more than one
env-entry-name elements with the same name as content.
 
Now I want to include the different env-entry-value contents depending of
the env-entry-name element cols attribute value, if there is not such an
attribute, i.e. like in the third env-entry, the default content of
env-entry-value should be included.



My XSL looks like this at the moment:

--- snip ---
<xsl:template name="include">
	<xsl:param name="key" />
	<xsl:variable name="includefile"
select="document('c:/dev/fbs/application/web/layouts/xml_to_jsp_include_02.x
ml')" />	
	<xsl:value-of
select="$includefile/properties/env-entry/env-entry-value[preceding-sibling:
:env-entry-name = $key and preceding-sibling::env-entry-name[@cols =
$cols]]" disable-output-escaping="yes" />
</xsl:template>	
--- snip  ---


where the $key variable is passed like "Name1", the $cols variable is
defined as a top level variable.

Here I have the problem that if an env-entry-name element has no cols
attribute, the depending content is not included. So I have to check if
there is an attribute or not, if not include the default content, if yes
include the specific content that fit to the cols attribute value.




Sebastian Schirmer

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


Current Thread