RE: [xsl] Checking whether a child node exists with specified attribute value.

Subject: RE: [xsl] Checking whether a child node exists with specified attribute value.
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 22 Apr 2009 19:45:38 +0100
<xsl:if test="addrField/@name = 'address1'">

This relies on the XPath feature that when comparing two sets, the result is
true if any item in the first set matches any item from the second.
("Existential comparison"). 

Or you may be more comfortable writing it as

<xsl:if test="addrField[@name = 'address1']">

which relies on the fact that in a boolean context, an empty node-set is
false, while a non-empty one is true.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: shawn.milo@xxxxxxxxx [mailto:shawn.milo@xxxxxxxxx] On 
> Behalf Of Shawn Milochik
> Sent: 22 April 2009 19:23
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Checking whether a child node exists with 
> specified attribute value.
> 
> Hi everybody. I'm an experienced developer just getting into 
> XSLT for the first time. I've managed to find solutions to 
> all my issues with Google searches so far, but not this time. 
> I searched the list archive, but I didn't find what I needed, 
> probably because I'm too new to this to know what keywords or 
> phrases to use.
> 
> What I want to do is check whether a node with multiple 
> children, all containing attribute "name," happens to have a 
> child where the "name"
> attribute is "address1."
> 
> The purpose is to dynamically generate HTML for an address 
> box from the database. The database table contains some 
> fields which may or may not need to be displayed. This is 
> decided in the XML.
> 
> Example:
> 
> Here is a set of working, related snippets I'm using, just to 
> explain what I'm trying to do:
> 
> XML:
>       <dataElement type="text" name="txtCorpRegNumber"
> label="Corporate Registration Entity No." required="yes"
> dbfield="corpRegEntityNum"/>
>       <dataElement type="text" name="txtFedTaxID" 
> label="Federal Tax ID" required="yes" dbfield="fedTaxID"/>
> 
> XSLT:
>               <xsl:if test="@type='text'">
>                 <input type=text id="{@name}" name="{@name}" 
> Text="{.}" />
>               </xsl:if>
> 
> 
> 
> 
> Here's the broken part:
> 
> XML:
>       <dataElement type="address" name="addrProjectOwner"
> dbfield="projOwnerAddress">
>         <addrField name="orgName" />
>         <addrField name="address1" />
>         <addrField name="address2" />
>         <addrField name="city" />
>         <addrField name="state" />
>         <addrField name="zip" />
>       </dataElement>
> 
> XSLT (please help fix):
> 
> (At this point I'm iterating through all <dataElement> nodes 
> in the XML) <xsl:if test="@type='address'">
>     <xsl:if test="if an addrField in my children has name 
> 'address1''">
>         <input type="text" id="txtAddress1" />
>     </xsl:if>
>     <xsl:if test="if an addrField in my children has name 'salami''">
>         <input type="text" id="txtSalami" />
>     </xsl:if>
> </xsl:if>
> 
> 
> 
> 
> Thanks,
> Shawn

Current Thread