[xsl] Conditional select

Subject: [xsl] Conditional select
From: "Tom Bueckers" <Tom.Bueckers@xxxxxxxxxxxx>
Date: Thu, 27 Oct 2005 16:27:26 -0500
Not sure how to define the problem but here goes.

I am having trouble writing out an optional element only if a condition
exists. I only want to display the indicators element IF and Only IF it
finds
a matching target. I cant figure out how to code this so that it only
writes out that tag if it finds a target and then writes out the
<indicator><name> as specified for each indicator that it finds.

I am not very good with templates and I assume that there is some
combination of templates that I could use to get this to work and I also
assume that this is a simple fix for the gurus our there but I am at a
loss.

Thanks for your help
Tom

<!-- my source XML File -->

<assets>
 <asset ID="A1001001A05J27B34524B67807">
    <entity>OFFER_PROFILES</entity>
    <metadata attribute="OFF_PRF_ID_NO" namespace="User">1442</metadata>
    <metadata attribute="OFF_PRF_NM" namespace="User">Request</metadata>

	<relationship target="1" />
    	<relationship target="2" />
    	<relationship target="3" />

 </asset>

 <asset ID="1">
    <entity>NONPOEINDICATOR</entity>
    <metadata attribute="INDICATOR_CD" namespace="User">met1</metadata>
    <metadata attribute="INDICATOR_NM" namespace="User">Name 1/metadata>
    <metadata attribute="INDICATOR_TX" namespace="User">the
application?</metadata>
  </asset>
 <asset ID="3">
    <entity>NONPOEINDICATOR</entity>
    <metadata attribute="INDICATOR_CD"
namespace="User">ctionAvailable</metadata>
    <metadata attribute="INDICATOR_NM" namespace="User">Name
3</metadata>
    <metadata attribute="INDICATOR_TX" namespace="User"> of the
application?</metadata>
  </asset>

</assets>


<!-- MY TRANSFORM -->

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:fn="http://www.w3.org/2005/02/xpath-functions";
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes";>

	<xsl:template match="/assets">

<request>


		<!-- Indicators -->
		<!-- select only the indicators linked to the offer
profile -->
		<indicators xmlns=""> <!-- this is optional and should
only be displayed if an indicator is linked -->

			<xsl:for-each select="asset[entity =
'OFFER_PROFILES']/relationship">
				<xsl:variable name="tmpID"><xsl:value-of
select="@target" /></xsl:variable>

				<!-- for each indicator -->
				<!-- traverses through every
NONPOEINDICATOR to find a match -->
				<xsl:for-each select="//asset[entity =
'NONPOEINDICATOR']">
					<!-- if the relationship target
equals the asset ID then we have found a match -->
					<xsl:if test="$tmpID = @ID">
						<indicator
xmlns=""><name xmlns=""><xsl:value-of select="metadata[@attribute =
'INDICATOR_NM']" /></name></indicator>
					</xsl:if> <!-- if found a
matching ID -->
				</xsl:for-each> <!-- for each indicator
-->
			</xsl:for-each> <!-- for each relationship tag
-->

		</indicators>

</request>

	</xsl:template>
</xsl:stylesheet>

<!-- current output if it finds targets -->
<request>
	<indicators xmlns="">
		<indicator>
			<name>Name 1</name>
		</indicator>
		<indicator>
			<name>Name 3</name>
		</indicator>
	</indicators>
</request>

<!-- current output if no targets are found -->
<!-- this is the problem, how do i structure the xsl so that it only
writes out this tag if and only if it find a target that matches an
asset ID of type NONPOEINDICATOR? -->

<request>
	<indicators xmlns="" \>
</request>

Current Thread