Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if an element exist

Status
Not open for further replies.

loosecannon1

Programmer
Feb 19, 2002
55
0
0
US
Hi all,

How can I determine if the <ext> element exists in the following XML File using XSL?

<client_list>
<client>
<name>
<first>David</first>
<middle>A.</middle>
<last>Peters</last>
</name>
<phone type=&quot;Work&quot;>
<area>653</area>
<exchange>555</exchange>
<num>4234</num>
<ext>124</ext>
</phone>
</client>
<client_list>

Thanks
 
Do a selectSingleNode and see if you get anything back.

Chip H.
 
If you want to write a separate XSL file instead of using the DOM, try the <xsl:if> function. It's format is <xsl:if test=&quot;your test here...&quot;>XSL code to be processed if test is true</xsl:if>

Here is an example based on your XML (using if in two places, phone ext and middle name:
Code:
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] xml:space=&quot;preserve&quot;>
<xsl:template match=&quot;name&quot;>
	Name: <xsl:value-of select=&quot;first&quot;/> <xsl:if test=&quot;middle&quot;><xsl:value-of select=&quot;middle&quot;/> </xsl:if><xsl:value-of select=&quot;last&quot;/></xsl:template>
<xsl:template match=&quot;phone&quot;>
	Type: <xsl:value-of select=&quot;@type&quot;/>
	Number: (<xsl:value-of select=&quot;area&quot;/>)<xsl:value-of select=&quot;exchange&quot;/>-<xsl:value-of select=&quot;num&quot;/><xsl:if test=&quot;ext&quot;> ex. <xsl:value-of select=&quot;ext&quot;/></xsl:if></xsl:template>
</xsl:stylesheet>

This is using XSL with a procedural language (apply procedures to data) mindset -- apply the if function to the data. However, XSL is a functional language (not procedural as most of us are familiar with), and it is tied strongly to matching templates. If you want something to be done if the ext element is there, create a template for it. If the element is not there, the template won't be called. Here is a different, template-based XSL file that does the same thing:

Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] xml:space=&quot;preserve&quot;>
	<xsl:template match=&quot;name&quot;>
	Name: <xsl:apply-templates/></xsl:template>
	<xsl:template match=&quot;first&quot;><xsl:apply-templates/> </xsl:template>
	<xsl:template match=&quot;middle&quot;><xsl:apply-templates/> </xsl:template>
	<!--<xsl:template match=&quot;last&quot;><xsl:apply-templates/></xsl:template>-->
	<xsl:template match=&quot;phone&quot;>
	Type: <xsl:apply-templates select=&quot;@type&quot;/>
	Number: <xsl:apply-templates/></xsl:template>
	<xsl:template match=&quot;@type&quot;><xsl:value-of select=&quot;.&quot;/></xsl:template>
	<xsl:template match=&quot;area&quot;>(<xsl:apply-templates/>) </xsl:template>
	<xsl:template match=&quot;exchange&quot;><xsl:apply-templates/>-</xsl:template>
	<!--<xsl:template match=&quot;num&quot;><xsl:apply-templates/></xsl:template>-->
	<xsl:template match=&quot;ext&quot;> ex. <xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

Notice that each template only applies the formatting for that element. Also notice that <xsl:apply-templates> is called to output the text as well. I should have avoided using a select on my apply-template to call the phone type attribute, since I'm selecting and not relying on pattern matching, but I didn't have time to figure out how to not have it process the entire element tree under phone a second time (producing a second phone number).

Which is best? Depends on what you are most comfortable with. (as is usually the case when asked this question!)
 
Thanks both of y'all for the responses. At this point I'm more intersested w/solving the problem w/XSL. I'm going to process chyros post and try to make it work w/my true application.

thks
 
Another tip.. in xslt there is a function called exists() which will determine if there are nodes matching an xpath expression..

example:

<xsl:if test=&quot;exists(ext)&quot;>
.. do yer code ..
</xsl:if>


hope that helps.

matt
 
ack dont listen to me. i was a bit confused.

ignore the exists() function, you can check if a node exists in a context just by trying to run the xpath expression as a check in an if statement like this:

<xsl:if test=&quot;ext&quot;>
.. do stuff..
</xsl:if>


sorry bout that.

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top