tags:

views:

80

answers:

2

Hi, I need to filter a XPath expression to grab only a certain attribute as not empty.

I tried this:

<xsl:template match="DocumentElement/QueryResults[string(@FileName)]">

and this:

<xsl:template match="DocumentElement/QueryResults[string-length(@FileName)>0]">

but it did not work. I need the same kind of data returning from the folloing XPath expression...

<xsl:template match="DocumentElement/QueryResults">

... but filtered to avoid items with empty attribute @FileName.

Thanks!

+1  A: 

Since FileName is a child element and not an attribute, you need to access it as such and not use the attribute qualifier @ in front of the node name.

Try:

<xsl:template match="DocumentElement/QueryResults[FileName]">

This will select the DocumentElement/QueryResults elements that have a FileName child element.

If, however, you always have a FileName child element (sometimes empty) and you want to select the non empty ones, try this:

<xsl:template match="DocumentElement/QueryResults[string-length(FileName) &gt; 0]">
Oded
Os, sorry, I just found "FileName" isn`t and attribute, but actually a node. How can I check for it not being empty? Thanks.
Marcos Buarque
@Marcos - That would explain the problems you have had... Answer updated (just remove the attribute qualifier @).
Oded
Thanks, I am not a XPATH specialist and I was having problems with the strange behavior of the software I am using and the parser. Since the source data was being pulled through an SQL query, I have applied a WHERE clause to it, instead of using a filter in the XPATH expression. Thank you.
Marcos Buarque
+1  A: 
<xsl:template match="DocumentElement/QueryResults[FileName != '']">

That's just a quick guess, and I haven't worked with XPath/XSLT in a long time. Still, if it's empty, then that should skip over it. While I prefer to use the functions like string-length, not all UAs support them (notably client-side XSLT parsers that barely work with XPath and XSLT 1.0 at all, nevermind the useful functions and functionality that XSLT 2.0 and XPath provide).

Dustin
Thanks, I am not a XPATH specialist either and I was having problems with the strange behavior of the software I am using and the parser. Since the source data was being pulled through an SQL query, I have applied the filter to it, instead of using a filter in the XPATH expression. Thanks anyway.
Marcos Buarque