xsl-list
[Top] [All Lists]

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

2009-04-22 14:46:10
<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[(_at_)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(_dot_)milo(_at_)gmail(_dot_)com 
[mailto:shawn(_dot_)milo(_at_)gmail(_dot_)com] On 
Behalf Of Shawn Milochik
Sent: 22 April 2009 19:23
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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="{(_at_)name}" name="{(_at_)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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--