Re: [xsl] Trouble matching only elements with a specific combination of attributes in a child element.

Subject: Re: [xsl] Trouble matching only elements with a specific combination of attributes in a child element.
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Wed, 16 Oct 2002 22:35:45 +0200
Hello Bennett,

> <xsl:template match="object[metadata/app_data/@name = 'Type' and
>                             metadata/app_data/@value = 'RFI']">

the above does not work like you expect it, because both expressions in the predicate are evaluated separately. So it is searched for a metadata/app_data, where the @name is 'Type', and one metadata/app_data, where @value is 'RFI'. Both can be found in both objects.

You must refer the @name and @value to the same app_data:

<xsl:template match="object[metadata/app_data[@name = 'Type' and [@value = 'RFI']]">

Regards,

Joerg

Bennett Smith wrote:
Hello,

I have an xml file that is structured something like

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<object id="00125">
		<metadata>
			<app_data name="Type" value="RFI"/>
			<app_data name="Version_Major" value="1"/>
		</metadata>
	</object>
	<object id="00126">
		<metadata>
			<app_data name="Type" value="Video"/>
			<app_data name="Category" value="RFI"/>
		</metadata>
	</object>
</root>

I am trying to build a style sheet that will match object elements that
contain a child app_data element where the app_data element has a name
of "Type" and a value of "RFI".  Here is the style sheet I am trying to
use.  It has a problem that I cannot figure out.  It matches any object
that has any app_data with a name of "Type" and with any other app_data
with a value of "RFI".  As a result, both of the objects in my sample
XML file are matched.  I could use some help with creating the Xpath
expression to matchin only the first object.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
	
	<xsl:template match="/">
		<xsl:element name="Assets">
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="object[metadata/app_data/@name = 'Type' and
metadata/app_data/@value = 'RFI']">
		<xsl:element name="Asset">
			<xsl:value-of select="@id"/>
		</xsl:element>
	</xsl:template>
	
</xsl:stylesheet>

Many thanks.

-- Bennett


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


Current Thread