Marcos Buarque
Marcos Buarque

Reputation: 3418

XPath filter not empty child element

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!

Upvotes: 6

Views: 14754

Answers (2)

Dustin
Dustin

Reputation: 1966

<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).

Upvotes: 3

Oded
Oded

Reputation: 499002

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]">

Upvotes: 8

Related Questions